MoreiraWebMaster
MoreiraWebMaster

Reputation: 73

Sort a list by multiple fields

I have problem where sort one list with more one field.

necessary to organize two courses simultaneously by color first and then the banana position.

Names of entities are only for example.

 private List<Banana> OrderProduct(List<Banana> list)
    {
        list.Sort(
            delegate(Banana b1, Banana b2)
            {
                return b1.Green.CompareTo(b2.Banana) && b1.Position.CompareTo(b2.Position);
            });
        return lista;
    }

Upvotes: 1

Views: 3216

Answers (1)

Ondrej Janacek
Ondrej Janacek

Reputation: 12616

Here you go

bananas.Sort(
    delegate(Banana b1, Banana b2)
    {          
        int res = b1.Color.CompareTo(b2.Color);
        return res != 0 ? res : b1.Position.CompareTo(b2.Position);
    });

Upvotes: 2

Related Questions