How could I sort a list of records, from a certain attribute of my class in C#

Asked

Viewed 64 times

1

How could I sort a list of records, from a certain attribute of my class as "Code"?

My class being next:

private int Codigo;
private string Nome;
private float SalarioMensal;
private string CPF;
private int Idade;

//Metodo CONSTRUTOR 1
public CEmpregado()
{
    Codigo = 0;
    Nome = "";
    SalarioMensal = 0.0f;
    CPF = "";
    Idade = 0;
}

//Metodo CONSTRUTOR 2
/// <summary>
/// Novo empregado
/// </summary>
/// <param name="_Codigo"> Codigo do Empregado</param> 
/// <param name="_Nome"> Nome do empregado</param> 
/// <param name="_SalarioMensal"> Salario mensal do empregado</param> 
/// <param name="_CPF"> CPF do empregado</param>
/// <param name="_Idade"> Idade do empregado</param>
public CEmpregado (int _Codigo, string _Nome, float _SalarioMensal, string _CPF, int _Idade)
{
    this.Codigo = _Codigo;
    this.Nome = _Nome;
    this.SalarioMensal = _SalarioMensal;
    this.CPF = _CPF;
    this.Idade = _Idade;
}

//Definindo os metodos SET
public void setCodigo(int Codigo)
{
    this.Codigo = Codigo;
}
public void setNome(string Nome)
{
    this.Nome = Nome;
}
public void setSalarioMensal(float SalarioMensal)
{
    this.SalarioMensal = SalarioMensal;
}
public void setCPF(string CPF)
{
    this.CPF = CPF;
}
public void setIdade(int Idade)
{
    this.Idade = Idade;
}

//Definindo os metodos GET
public int getCodigo()
{
    return this.Codigo;
}
public string getNome()
{
    return this.Nome;
}
public float getSalarioMensal()
{
    return this.SalarioMensal;
}
public string getCPF()
{
    return this.CPF;
}
public int getIdade()
{
    return this.Idade;
}

To complement the question, I add a record in the class like this:

I create a list:

CEmpregado empregado = new CEmpregado();

Add in class:

ListaEmpregados.Add(empregado);

  • You want to order the ListaEmpregados?

  • Positive I have the list of employees, with n employees, each employee have these 5 attributes and need to sort them by a certain attribute in the case of the "Code" that the user himself informs for each employee

1 answer

4

To sort a list you can import the namespace System.Linq and use the OrderBy or the OrderByDescending, it will return an ordered list, exemplifying:

var listaOrdenada = ListaEmpregados.OrderBy(p=> p.Codigo);
var outraListaOrdenada = ListaEmpregados.OrderByDescending(p=> p.Codigo);

Note that as a parameter you pass the property you should sort.

The code added in the question does not appear to be C#, it seems that it was imported from another language. I suggest reading Nomenclature standard in code for C#

  • I think it would be worth quoting the method Sort, if she wanted to sort her own list..

  • This method had tried to use, but it occurs to me an error in Orderby: (CS0411 C# The type Arguments for method cannot be inferred from the Usage. Try specifying the type Arguments explicitly.)

  • @Bianca How did you try to use it? Could you add in the question, please?

Browser other questions tagged

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