How to Sort a List in C#?

C# List Sort method featured image

As creating a List<T> in C#, its items may be in random order whether added by using the Add or Insert methods.

  • In this tutorial, I will show you how to sort a list in ascending or descending order by using the C# List Sort method.
  • The tutorial also covers using the Linq with orderby keyword.

Sorting in ascending order by List Sort method

Using the Sort method of the List<T> class is simple. You may specify the List<T> name as follows:

myList.sort()

This will sort the myList items in ascending order.

To demonstrate that, we created a list of int type in the program below.

The list items are added in random order which is followed by using the the Sort method.

See the List before and after using the Sort method:

using System;

using System.Collections.Generic;
class sort_example

{
    static void Main()
    {
        List<int> sort_list = new List<int>();
        sort_list.Add(11);
        sort_list.Add(31);
        sort_list.Add(5);
        sort_list.Add(7);
        sort_list.Add(9);
        sort_list.Add(15);
        sort_list.Add(1);
        sort_list.Add(3);

        System.Console.WriteLine("List Before");

        foreach (int num in sort_list)
        {
          System.Console.WriteLine(num);
        }

        //Now sorting the list
        sort_list.Sort();
        System.Console.WriteLine("\n" + "List After" + "\n");

        foreach (int num in sort_list)
        {
        System.Console.WriteLine(num);
       }
        Console.ReadLine();
    }
}

The result:

c sharp sort-list

Sorting the list in descending order example

A trick for sorting the List<T> in descending order is using the reverse() method.

The reverse() method is used to reverse the order of elements in the entire list (learn more here).

In that case, first, use the sort() method that will order the list in ascending order. After that, use the reverse() method for sorting that list in descending order.

The example below shows how:

using System;

using System.Collections.Generic;

class sort_desc_example
{
    static void Main()
    {
        List<int> sort_desc = new List<int>();
        sort_desc.Add(8);
        sort_desc.Add(16);
        sort_desc.Add(14);
        sort_desc.Add(4);
        sort_desc.Add(26);
        sort_desc.Add(49);
        sort_desc.Add(2);
        sort_desc.Add(50);

        System.Console.WriteLine("Original List");

        foreach (int num in sort_desc)
        {
            System.Console.WriteLine(num);
        }

        //Now sorting the list
        sort_desc.Sort();

        //Reversing the sorted list
        sort_desc.Reverse();

        System.Console.WriteLine("\n" + "List in Desc Order" + "\n");

        foreach (int num in sort_desc)
        {
            System.Console.WriteLine(num);
        }
        Console.ReadLine();
    }
}

The output:

c sharp sort list desc

This is how it worked.

  • The Reverse() method is not for sorting the results in descending order. Instead, it simply reverses the order of items for the List<T>.
  • The use of Sort() method will bring the List<T> items in ascending order and so the reverse() will change the order that is descending.

Using LINQ orderby keyword for sorting a list

The other way of sorting a List<T> is using the Linq in C#. For that, you may use the OrderBy keyword as shown in the example below.

I created a List<T> of string elements and then used the LINQ OrderBy to sort the elements in ascending order:

using System;

using System.Collections.Generic;

using System.Linq;

class sort_example
{
    static void Main()
    {
        List<string> sort_linq_lst = new List<string>() { "def", "abc", "xyz", "lmn" };
        sort_linq_lst = sort_linq_lst.OrderBy(p => p.Substring(0)).ToList();

        System.Console.WriteLine("List After Sorting by LINQ\n");

        foreach (var num in sort_linq_lst)
        {
            System.Console.WriteLine(num);
        }
        Console.ReadLine();
    }
}

The result:

c sharp sort linq

Another way of using LINQ for ascending-order sorting

The following example shows sorting the numeric list items in ascending order by using the Linq with orderby keyword in another way. Have a look:

using System;

using System.Collections.Generic;

using System.Linq;

class sort_example_Linq
{
    static void Main()
    {
        List<int> sort_num_Linq = new List<int>() { 5, 3, 1, 4, 9 };

        var aft_sort = from x in sort_num_Linq
            orderby x
            select x;
        Console.WriteLine("List after sorting by Linq:\n");

        foreach (var z in aft_sort)
        {
            Console.WriteLine(z);
        }
        Console.ReadLine();
    }
}

The result:

1

3

4

5

9

Sorting in descending order example

By adding the descending keyword in with the orderby, you may get the list sorted in descending order.

See the code and output below:

using System;
using System.Collections.Generic;
using System.Linq;

class sort_example_Linq
{
    static void Main()
    {
        List<int> sort_num_Linq_desc = new List<int>() { 5, 15, 3, 99, 11 };
        var aft_sort = from y in sort_num_Linq_desc
                       orderby y descending
                       select y;
        Console.WriteLine("List after sorting by Linq:\n");

        foreach (var y in aft_sort)
        {
            Console.WriteLine(y);
        }
        Console.ReadLine();
    }
}

The result:

99

15

11

5

3

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!