Orderby in lists<T>

Asked

Viewed 374 times

5

I have a list list<Pessoas>:

my class people:

public class Pessoas
{
    public String Nome { get; set; }
    public DateTime Nascimento { get; set; }
}

Theoretically I could make a OrderBy thus:

Pessoas.OrderBy(p => p.Nome);

But the OrderBy to do this. My class has to 'be' or 'implement' something from IEnumerable?

  • I found it curious that the other answers gave a solution using List. Is this what you want? I mean, you don’t want your own class to keep the list of people?

  • That’s what I understood, since the question started with "I have a list list<Pessoas>".

  • @I don’t want my class to keep the list of people, I want to be able to order a list<Pessoas>, moving to a var or to another list<Pessoas>.

  • 1

    I have already adapted my answer by giving more information to this case and by saying that the nomenclature you are using makes confusing what you are saving in the class. It is your discretion to use as you wish but this confusion shows how well-chosen names are easier to communicate intent.

4 answers

5

OrderBy is a Extension method in namespace System.Linq. If you have it on your list of usings, then the method should appear.

using System;
using System.Collections.Generic;
using System.Linq;

public class Pessoas {
    public String Nome { get; set; }
    public DateTime Nascimento { get; set; }
}

public class Teste {
    public static void Main() {
        List<Pessoas> lista = new List<Pessoas>();
        lista.Add(new Pessoas { Nome = "Maria", Nascimento = DateTime.Today });
        lista.Add(new Pessoas { Nome = "Jose", Nascimento = DateTime.Today });
        var ordenada = lista.OrderBy(p => p.Nome);
        foreach (var pessoa in ordenada) {
            Console.WriteLine(pessoa.Nome);
        }
    }
}

5


Yes, your class needs to implement the interface IEnumerable. In addition you have to make available the namespace System.Linq.

You’re calling the class Pessoas, this means that it will contain several people. So it seems to me that it makes sense to implement this interface. But if your intention is that this class represents only one person, there would not make sense and would have to store the lists of people in another object, maybe a list, as shown in the Carlosfigueira response. If your intention is to use a list to store people, then the list already implements the IEnumerable and you would only need to use the namespace of LINQ. But if so, the class name gives a wrong indication of what it represents.

Anyway you can not call this method from the class itself, as in the example shown (I think). It has to be done with one instance. It may even be an instance, since C# allows variables to have names equal to types, but the name thus does not give a good indication of what it is. So you probably want it to be a var pessoas = new List<Pessoa>();.

How LINQ is implemented over extension methods, these are only available when you do so explicitly. That is, LINQ methods will only be considered part of your object if they were made available with using, otherwise they do not appear in Intellisense since they are not part of the object, they are auxiliary definitions apart and optional.

3

If you are using the form below all you need to do is add one using System.Linq at the beginning of the file.

public class Pessoas
{
    public String Nome { get; set; }
    public DateTime Nascimento { get; set; }
}

var Pessoas = new List<Pessoas>();

Pessoas.OrderBy(x => x.Nome);

If you are using so just need the namespace right.

0

var lOrdenada = lPessoas.OrderBy(p => p.Nome);

ou

var lOrdenada2 = (from pessoa in lPessoas orderby pessoa.Nome select pessoa).ToList();

Browser other questions tagged

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