Sort List by by descending values

3

I have a Chromosome object :

public List<int> Rotas { get; set; }
public int ValorFitness { get; set; } 

And I also have a list of this object List<Cromossomo> .

I would like to sort this list by the property ValorFitness , in descending order.

I'm using C #, WindowsForm, DotNet 4.5.1

    
asked by anonymous 09.07.2014 / 15:27

2 answers

3

You can use .OrderByDescending

Example:

var listaOrdenada = cromossomos.OrderByDescending(e => e.ValorFitness);
    
09.07.2014 / 15:52
3

Using linq (Language Integrated Query) and Lambda .

An example:

List<Cromossomo> comossomosOrdenados = cromossomos.OrderByDescending(c => c.ValorFitness);

Another example:

List<Cromossomo> comossomosOrdenados = (from c in cromossomos
        orderby c.ValorFitness descending
        select c).ToList();

This link has some Linq examples.

    
09.07.2014 / 15:52