J
J
justslipknot2018-03-25 11:59:18
.NET
justslipknot, 2018-03-25 11:59:18

How to make multiple sorts in one class?

Actually, subject. How to implement sorting in a class by several fields to choose from. For example, sorting by name, by age, etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Silin, 2018-03-25
@byme

var orderedUsers = users.OrderBy(o => o.Name).ThenBy(o => o.Age);

F
freeExec, 2018-03-25
@freeExec

public static int CompareToByNameAge(Pers p1, Pers p2)
        {
            int v = p1.Name.CompareTo(p2.Name);

            if (v == 0)
            {
                int t = p1.Age.CompareTo(p2.Age);
                return t;
            }
            return v;
        }

list.Sort(Pers.CompareToByNameAge);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question