-2
How do I sort a complex list ?
I have this list below and I need to sort by Nometype and Nomesubtype:
public class Tipo()
{
public int IdTipo {get; set;}
public string NomeTipo {get; set;}
public List<SubTipo> NomeSubTipo {get; set;}
}
public class SubTipo()
{
public int IdTipo {get; set;}
public int IdSubTipo {get; set;}
public string NomeSubTipo {get; set;}
}
List<Tipo> ListaTipo = new List<Tipo>();
ListaTipo[0].NomeTipo = "LISTA XYZ"
ListaSubTipo[0].NomeSubTipo = "B"
ListaSubTipo[1].NomeSubTipo = "A"
ListaSubTipo[2].NomeSubTipo = "C"
ListaTipo[1].NomeTipo = "LISTA BLA BLA"
ListaSubTipo[0].NomeSubTipo = "B"
ListaSubTipo[1].NomeSubTipo = "C"
ListaSubTipo[2].NomeSubTipo = "A"
ListaTipo[2].NomeTipo = "LISTA TATATA"
ListaSubTipo[0].NomeSubTipo = "C"
ListaSubTipo[1].NomeSubTipo = "B"
ListaSubTipo[2].NomeSubTipo = "A"
List<Tipo> lreturn = new List<Tipo>();
lreturn = ListaTipo;
lreturn.OrderBy(x => x.NomeTipo).OrderBy(x => x.ListaSubTipo.OrderBy(m => m.NomeSubTipo)).ToList();
The following error occurs:
At least one object must implement IComparable.
You’re creating list classes that only have the name. They contain lists, this is totally different, either turn them into lists or compare only the lists. Whatever?
– Maniero
Exactly.. the error you are getting is not quite because you want to sort by more than one column.. https://stackoverflow.com/questions/14141891/at-least-one-object-must-implement-icomparable
– rLinhares
@Maniero I think my doubt was not clear edit the post I believe it was clearer.
– hard123