Sorting List<T> in .NET

Muhammad Waseem
Become .NET Pro !
2 min readFeb 12, 2023

💡 How to sort List<string> in C# ?

1. Using sort method you can sort list in ascending order.

2. Using reverse method of list we can sort list in descending order provided that list is already sorted.

3. Using order by method of LINQ we can sort list in ascending order.

4. Order by descending method available in LINQ sorts the list in descending order.

I have ran the benchmarks for 1000 items of List and you can see the benchmarks , sorting by Reverse method was most efficient available in .NET 7.

💡 Sort List by Property in .NET

♉ We can use 𝐎𝐫𝐝𝐞𝐫𝐁𝐲and 𝐎𝐫𝐝𝐞𝐫𝐁𝐲𝐃𝐞𝐬𝐜𝐞𝐧𝐝𝐢𝐧𝐠 to Sort List on single property ascending and descending respectively.

♉ If we want to sort list on multiple properties then we can 𝐓𝐡𝐞𝐧𝐁𝐲 to apply ordering on more than one property.

If you want to help the author in growing

  1. Subscribe my Weekly .NET Newsletter of C#/.NET
  2. Follow me on LinkedIn/Twitter OR Clap at least 50 times.
  3. Get exclusive .NET Questions & Answers by becoming a Patron.
  4. Download my eBook at Gumroad that contains 30 .NET Tips.

--

--