Your program stores data in ArrayList collections and you want to sort the individual elements in these collections. The Sort and Reverse methods on ArrayList are ideal for this purpose, but it is helpful to see an example and understand what the methods do.


Sorting an ArrayList is done in many programs that use ArrayLists, as sorting provides a view of the data that is helpful for both users and computers. The Sort method in the base class libraries is a parameterless instance method on ArrayList. It works on different element types, and the example here shows strings.
Program that sorts ArrayList and reverses [C#]
using System;
using System.Collections;
class Program
{
static void Main()
{
//
// Create an ArrayList with four strings.
//
ArrayList list = new ArrayList();
list.Add("Cat");
list.Add("Zebra");
list.Add("Dog");
list.Add("Cow");
//
// Sort the ArrayList.
//
list.Sort();
//
// Display the ArrayList elements.
//
foreach (string value in list)
{
Console.WriteLine(value);
}
//
// Reverse the ArrayList.
//
list.Reverse();
//
// Display the ArrayList elements again.
//
foreach (string value in list)
{
Console.WriteLine(value);
}
}
}
Output
Cat
Cow
Dog
Zebra
Zebra
Dog
Cow
Cat
Notes. The above console program defines the Main entry point, and in Main an ArrayList is declared and populated with several strings. The Sort method is then called; its results are displayed next. Finally, the collection is reversed. You can see the foreach loops on ArrayList here as well.

Inside the base class libraries, you can see that the Sort method here always ends up in an internal TrySZSort or QuickSort method when it doesn't throw an exception. The TrySZSort internal method is optimized for one-dimensional arrays, also known as "Zero" arrays or vectors.
Performance of Sort. Because the TrySZSort method used in the base class libraries is implemented in native code, it has been heavily optimized. Therefore, this method is likely faster than any solution written in the C# language. In other words: using Sort on ArrayList or on Array is faster than most custom implementations.

Here we looked at how you can sort an ArrayList with the Sort instance method, and also reverse it. Although the List constructed type is faster, ArrayList collections are widely deployed and very useful. We looked into the base class libraries and found that the Sort method has many optimizations.
ArrayList Tips Sort Examples