Sorting in C#

There are many ways to perform sofisticated sorting in C# and there are many webpages covering that subject. This page will show how to perform the most basic type of sorting in C#. That is sorting an Array or ArrayList in ascending and descending order.

C# sorting examples Let’s look at some basic sorting examples:

string[] myStringArray = new string[3];
    myStringArray[0] = "b";
    myStringArray[1] = "a";
    myStringArray[2] = "c";
    // will sort in ascending order
    Array.Sort(myStringArray); 
    // reverses the order so now descending
    Array.Reverse(myStringArray); 

    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("b");
    myArrayList.Add("c");
    myArrayList.Add("a");
    // will sort in ascending order
    myArrayList.Sort(); 
    // reverses the order so now descending
    myArrayList.Reverse();