Tengo un ejemplo, es más o menos así (no sé VB así que te lo puse en C#)
class Program: IComparer<Entidad>
{
static void Main(string[] args)
{
Program p = new Program();
p.Test();
}
public void Test()
{
List<Entidad> entidades = new List<Entidad>();
entidades.Add(new Entidad(2, "segunda", "nombre 2",
DateTime.Now.AddHours(1)));
entidades.Add(new Entidad(1, "primera", "nombre 1", DateTime.Now));
entidades.Add(new Entidad(3,"Tercera","nombre 3",
DateTime.Now.AddHours(2)));
entidades.Sort(this);
}
#region IComparer<Entidad> Members
public int Compare(Entidad x, Entidad y)
{
//este método se llama con cada elemento y hace que lo
compare con el siguiente
//acá poné tu criterio de comparación
//tenés que devolver 1 si el primer elemento es mayor -1
si es menor y 0 si es igual
if (x.Id > y.Id) return 1;
if (x.Id < y.Id) return -1;
return 0;
}
#endregion
}
class Entidad
{
private int _id;
private string _descripcion;
private string _nombre;
private DateTime _fecha = new DateTime();
public Entidad() { }
public Entidad(int id, string descripcion, string nombre,
DateTime fecha)
{
this.Id = id;
this.Descripcion = descripcion;
this.Nombre = nombre;
this.Fecha = fecha;
}
public DateTime Fecha
{
get { return _fecha; }
set { _fecha = value; }
}
public string Nombre
{
get { return _nombre; }
set { _nombre = value; }
}
public string Descripcion
{
get { return _descripcion; }
set { _descripcion = value; }
}
public int Id
{
get { return _id; }
set { _id = value; }
}
}
On 6/1/07, Carlos S. Villalba <[EMAIL PROTECTED]> wrote:
Es verdad, justamente era lo que iba a recomendar. Me ahorraste usar mis
dedos!!!.
___________________________________________
Sebastian Villalba
Award Support S.R.L.
[EMAIL PROTECTED]
Tel/Fax: 4541-0503 y lineas rotativas
___________________________________________
-----Mensaje original-----
De: [email protected] [mailto:[EMAIL PROTECTED] En nombre de Leonardo
Micheloni
Enviado el: viernes, 01 de junio de 2007 12:54
Para: [email protected]
Asunto: [puntonet] Pregunta sobre ordenamiento
Estás usando .net 2? porque si es así yo usaría una lista genérica y
de un modo muy fácil haría un sort implementarndo IComparable con el
criterio que quiera.
On 6/1/07, Pata del Santo <[EMAIL PROTECTED]> wrote:
>
>
> yo la lista de parámetros la pasaría como un array de string, para no estar
> agregando parámetros opcionales en orderList
>
> ________________________________
> De: [email protected] [mailto:[EMAIL PROTECTED] En nombre de MYMTEC
> S.A. - Javier Wamba
> Enviado el: viernes, 01 de junio de 2007 16:08
> Para: [email protected]
> Asunto: [puntonet] Pregunta sobre ordenamiento
>
>
>
>
>
> Buen dia!
>
> Hoy tengo una pregunta con respecto a mi codigo.
>
> El problema que esto intenta resolver es ordenar una lista por propiedades
> de sus objetos por ejemplo podria tener un objeto:
>
> persona que tiene las propiedades fechaDeNacimiento y provincia( que es un
> objeto que contiene la propiedad id) entonces podria escribir
>
> funciones.orderList(listaDePersonas,"fechaDeNaciemiento
> DESC","provincia.id ASC")
>
> donde a partir del segundo parametro se define el criterio de ordenamiento.
>
> Queria saber si esta bien hecho o hay algo que ya lo hace y de forma
> optimizada.
>
> Y si alguien se pregunta porque lo hice asi: Es una de las tantas de mi
> jefe!
>
> Saludos y gracias,
>
> Javier Wamba
>
>
>
> Imports System
>
> Public Class Funciones
>
> #Region "Ordenamiento"
>
> Private Shared mutexOrden As New Threading.Mutex
>
> Private Shared ordenamientos() As String
>
> Public Shared Sub orderList(ByVal list As List(Of base), ByVal ParamArray
> orders() As String)
>
> mutexOrden.WaitOne()
>
> Try
>
> ordenamientos = orders
>
> list.Sort(AddressOf order)
>
> mutexOrden.ReleaseMutex()
>
> Catch ex As Exception
>
> mutexOrden.ReleaseMutex()
>
> End Try
>
> End Sub
>
> Private Shared Function devolverPropiedad(ByVal b1 As entidades.base, ByVal
> prop() As String, ByVal index As Integer) As Object
>
> Try
>
> If prop.Length - 1 = index Then
>
> Return b1.GetType.GetProperty(prop(index)).GetValue(b1, Nothing)
>
> Else
>
> Return devolverPropiedad(b1.GetType.GetProperty(prop(index)).GetValue(b1,
> Nothing), prop, index + 1)
>
> End If
>
> Catch ex As Exception
>
> Throw ex
>
> End Try
>
> End Function
>
> Private Shared Function orderAuxiliar(ByVal b1 As entidades.base, ByVal b2
> As entidades.base, ByVal index As Integer) As Integer
>
> Dim vec() As String
>
> Try
>
> vec = ordenamientos(index).Split(" ")
>
> If vec.Length = 2 Then
>
> Dim compar As Integer = devolverPropiedad(b1, vec(0).Split("."),
> 0).compareto(devolverPropiedad(b2, vec(0).Split("."), 0))
>
> If compar = 0 Then
>
> If ordenamientos.Length - 1 = index Then
>
> Return compar
>
> Else
>
> Return orderAuxiliar(b1, b2, index + 1)
>
> End If
>
> Else
>
> If vec(1).ToUpper = "DESC" Then
>
> Return compar * (-1)
>
> Else
>
> Return compar
>
> End If
>
> End If
>
> ElseIf vec.Length = 1 Then
>
> Dim compar As Integer = devolverPropiedad(b1, vec(0).Split("."),
> 0).compareto(devolverPropiedad(b2, vec(0).Split("."), 0))
>
> If compar = 0 Then
>
> If ordenamientos.Length - 1 = index Then
>
> Return compar
>
> Else
>
> Return orderAuxiliar(b1, b2, index + 1)
>
> End If
>
> Else
>
> Return compar
>
> End If
>
> Else
>
> Throw New Exception("Demasiados espacios")
>
> End If
>
> Catch ex As Exception
>
> Throw ex
>
> End Try
>
> End Function
>
> Private Shared Function order(ByVal b1 As entidades.base, ByVal b2 As
> entidades.base) As Integer
>
> Try
>
> Return orderAuxiliar(b1, b2, 0)
>
> Catch ex As Exception
>
> Throw ex
>
> End Try
>
> End Function
>
> #End Region
>
> End class
>
>
>
> Public Class base
>
> Friend Sub New()
>
> End Sub
>
> End Class