Thomas Hahn Thomas Hahn - 3 months ago 9
C# Question

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

I am getting a strange behavior with the

OrderBy()
Method of
Lists
.

Imagine you have a List and each Person has
Firstname
,
Lastname
and
Gender
.
If all People in this list are of
Gender
"m"
, I will get a different sorting after each call of

list.OrderBy(p => p.Gender)


But I don't want my list to jump around on each and every refresh. Any idea on this?

Answer

You can use ThenBy() to sort it using other properties if Gender is same. Like this:

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