C# ArrayList - Sort() Method



The C# ArrayList Sort() method is used to sort the element in the ArrayList or a portion of the ArrayList.

Syntax

Following are the syntax of the C# ArrayList Sort() method −

This syntax will sort the elements in the entire ArrayList.

public virtual void Sort();

This syntax will sort the elements in the entire ArrayList using the specified comparer function.

public virtual void Sort(System.Collections.IComparer? comparer);

This syntax will sort the elements in a range of elements in ArrayList using the specified comparer function.

public virtual void Sort(int index, int count, System.Collections.IComparer? comparer);

Parameters

This method accepts the following parameters according to all overloaded methods −

  • comparer: It is an IComparer implementation to use when comparing elements.
  • index: It is starting index of the range to sort.
  • count: It is the length of the range to sort.

Return value

This method does not return any value. It's return type is void.

Example 1: Sort the Entire ArrayList

Following is the basic example of the Sort() method to sort the element in the ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 3, 2, 5, 1, 4};
      
      Console.Write("Before Sort: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      arrayList.Sort();
      Console.Write("\nAfter Sort: ");
      foreach(var item in arrayList){
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Sort: 3 2 5 1 4 
After Sort: 1 2 3 4 5

Example 2: Sort Strings in an ArrayList

The below example creates an ArrayList with some elements. We then use the Sort() method to sort the elements in the ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList {
          "The",
          "quick",
          "brown",
          "fox",
          "jumps",
          "over",
          "the"
      };
      
      Console.Write("Before Sort: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      arrayList.Sort();
      Console.Write("\nAfter Sort: ");
      foreach(var item in arrayList) {
          Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Sort: The quick brown fox jumps over the 
After Sort: brown fox jumps over quick the The

Example 3: Sort Using the Comparer

Here, in this example, we have an ArrayList, and we use the sort() method with comparer to sort the ArrayList in descending order −

using System;
using System.Collections;
class DesComparer : IComparer
{
   public int Compare(object x, object y)
   {
      return ((int)y).CompareTo((int)x);
   }
}

class Program
{
   static void Main()
   {
      ArrayList list = new ArrayList() { 5, 2, 8, 1, 3 };

      Console.WriteLine("Before Sorting:");
      foreach (var item in list)
      {
         Console.Write(item + " ");
      }

      list.Sort(new DesComparer());

      Console.WriteLine("\nAfter Sorting:");
      foreach (var item in list)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Sorting:
5 2 8 1 3 
After Sorting:
8 5 3 2 1 

Example 4: Sort in the Range

Here, in the following example shows how to sort the values in a range of elements in an ArrayList using the default comparer in Sort() method −

using System;
using System.Collections;
public class SamplesArrayList3
{
   public static void Main()
   {
      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add("The");
      myAL.Add("QUICK");
      myAL.Add("BROWN");
      myAL.Add("FOX");
      myAL.Add("jumps");

      Console.WriteLine("The ArrayList initially contains the following values:");
      PrintIndexAndValues(myAL);

      myAL.Sort(1, 3, null);
      Console.WriteLine("After sorting from index 1 to index 3 with the default comparer:");
      PrintIndexAndValues(myAL);
   }
   public static void PrintIndexAndValues(IEnumerable myList)
   {
      int i = 0;
      foreach (Object obj in myList)
         Console.WriteLine("\t[{0}]:\t{1}", i++, obj);
      Console.WriteLine();
   }
}

Output

Following is the output −

The ArrayList initially contains the following values:
	[0]:	The
	[1]:	QUICK
	[2]:	BROWN
	[3]:	FOX
	[4]:	jumps

After sorting from index 1 to index 3 with the default comparer:
	[0]:	The
	[1]:	BROWN
	[2]:	FOX
	[3]:	QUICK
	[4]:	jumps
csharp_arraylist.htm
Advertisements