How To Sort List Of Objects In .Net



TODO:

You have a list of objects (List<T>), let's just say List<Dog> for this example.  You want to sort them by a property called "Breed".  This example will show you just how to do that.

 

SOLUTION:

List myDogList = myDogList.OrderBy(d => d.Breed).ToList();
//or
List myDogList = myDogList.OrderByDescending(d => d.Breed).ToList();

 

NOTES:

Your list of dogs will now be sorted by Breed. 

*****Note that the default sort order is ascending.



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading