How to sort list with complex object by one of its properties?
I am with a list of objects and I want to sort the list by one of the properties (Desc of type string), how do I do that?
public class Foo
{
public string Desc { get; set; }
// Várias outras propriedades
}
public class Program
{
static void Main()
{
List<Foo> objetos = Program.PreencheObjetos();
objetos.Sort();
}
public static List<Foo> PreencheObjetos()
{
// Retorna uma lista preenchida.
}
}
I couldn't use Sort to do this.
16
5 answers
You can work with sorting using the List<T>.Sort(IComparer <T>)
method (documentation in MSDN) by implementing the IComparer
interface (documentation in MSDN) in the classes of the objects you want to sort. Here's how:
In the class of objects
public class ComparadorDoMeuObjeto: IComparer<MeuObjeto> {
int IComparer.Compare( MeuObjeto x, MeuObjeto y ) {
if(x == null)
return -1;
if(y == null)
return 1;
return x.PropriedadeInterna - y.PropriedadeInterna ;
}
}
See how to implement
+-------------------+--------------------+
| Valor | Significado |
+-------------------+--------------------+
| menor que zero | x é menor do que y |
| zero | x é igual a y |
| maior do que zero | x é maior do que y |
+-------------------+--------------------+
How to sort
List<MeuObjeto> objs= new List<MeuObjeto>();
objs.Add(new MeuObjeto(1));
objs.Add(new MeuObjeto(2));
objs.Add(new MeuObjeto(3));
objs.Add(new MeuObjeto(4));
ComparadorDoMeuObjeto comparador = new ComparadorDoMeuObjeto();
// Com objeto IComparer
objs.Sort(comparador);
// Com Lambda para fazer a mesma coisa (parâmetros do IComparer)
objs.Sort((x, y) => x.PropriedadeInterna - y.PropriedadeInterna);
9
Author: Alexandre Marcondes, 2014-02-10 19:57:10
You can use linq for this:
objetos = objetos.OrderBy(o => o.Desc).ToList();
Or passing a function in .Spell ()
objetos.Sort((o1,o2) => o1.Desc.CompareTo(o2.Desc));
12
Author: Joao Raposo, 2014-02-11 09:10:27
Try using Linq:
var objetosEmOrdem = (from o in objetos
orderby o.Desc
select s);
3
Author: RaphaelZ, 2014-02-10 17:19:15
Use the Sort
method by passing a delegate Comparison<T>
.
objetos.Sort((x, y) => x.Desc.CompareTo(y.Desc));
Http://msdn.microsoft.com/en-us/library/tfakywbh%28v=vs.110%29.aspx
2
Author: Doug, 2014-02-10 17:27:24
See if this is what you want
public class Foo
{
public int Codigo { get; set; }
public string Nome { get; set; }
public string Sobrenome { get; set; }
// Várias outras propriedades
}
public class Program
{
static void Main()
{
List<Foo> objetos = Program.PreencheObjetos();
objetos.OrderBy(a => a.Nome);
objetos.OrderBy(a => a.Codigo);
objetos.OrderBy(a => a.Sobrenome);
objetos.OrderByDescending(a => a.Nome);
objetos.OrderByDescending(a => a.Codigo);
objetos.OrderByDescending(a => a.Sobrenome);
}
public static List<Foo> PreencheObjetos()
{
List<Foo> list = new List<Foo>();
for (int i = 0; i < 10; i++)
{
list.Add(new Foo() { Codigo = i, Nome = "Nome " + i, Sobrenome = "Sobrenome " + i });
}
return list;
}
}
2
Author: user1013755, 2014-02-10 17:35:10