What is the result of List.OrderBy () if multiple entries have the same value?

I get weird behavior with a OrderBy()method Lists.

Imagine that you have a List, and each person has Firstname, Lastnameand Gender. If all the people on this list have Gender "m", after each call

I get another sort,
list.OrderBy(p => p.Gender)

But I do not want my list to hop on every update. Any idea on this?

+4
source share
3 answers

You can use ThenBy()to sort using other properties, if Paul is the same. Like this:

list.OrderBy(p=>p.Gender).ThenBy(p=>p.FirstName);
+5
source

Gender m , m , . , , m. , - , , FirstName.

list.OrderBy(p=>p.Firstname);

list.OrderBy(p=>p.Lastname);

list.OrderBy(p=>p.Gender).ThenBy(p=>p.FirstName);
+1

Sort by all fields, for example:

list.OrderBy(x=>x.Gender).ThenBy(x=>x.FirstName).ThenBy(x=>x.LastName);
+1
source

Source: https://habr.com/ru/post/1652986/


All Articles