How do I order a Complex List?

Asked

Viewed 50 times

-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.
  • 1

    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?

  • 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

  • @Maniero I think my doubt was not clear edit the post I believe it was clearer.

1 answer

5


I think you want it something like this:

List<Tipo> ListaTipo = new List<Tipo>();

ListaTipo.Add(new Tipo()
{
    NomeSubTipo = new List<SubTipo>()
    {
        new SubTipo() { NomeSubTipo = "B" },
        new SubTipo() { NomeSubTipo = "A" },
        new SubTipo() { NomeSubTipo = "C" }
    },
    NomeTipo = "LISTA XYZ"
});

ListaTipo.Add(new Tipo()
{
    NomeSubTipo = new List<SubTipo>()
    {
        new SubTipo() { NomeSubTipo = "B" },
        new SubTipo() { NomeSubTipo = "C" },
        new SubTipo() { NomeSubTipo = "A" }
    },
    NomeTipo = "LISTA BLA BLA"
});

ListaTipo.Add(new Tipo()
{
    NomeSubTipo = new List<SubTipo>()
    {
        new SubTipo() { NomeSubTipo = "C" },
        new SubTipo() { NomeSubTipo = "B" },
        new SubTipo() { NomeSubTipo = "A" }
    },
    NomeTipo = "LISTA TATATA"
});

List<Tipo> lreturn = ListaTipo.OrderBy(x => x.NomeTipo).
    ThenBy(y => y.NomeSubTipo.OrderBy(z => z.NomeSubTipo)).ToList();

I think the ThenBy solves your problem.

  • to which that is exactly I will adapt here to my code and I will give a feedback. thank you from now!

  • Has HERE a good explanation about the use of ThenBy.

  • Perfect @João Martins ! It worked ! Thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.