Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

list

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?

like image 883
Thomas Hahn Avatar asked Mar 12 '23 06:03

Thomas Hahn


1 Answers

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);
like image 164
Umair M Avatar answered Mar 30 '23 00:03

Umair M