C# | Sort the elements in the ArrayList

ArrayList.Sort Method is used to sorts the elements in the ArrayList. There are total 3 methods in the overload list of this method as follows:

  1. Sort()
  2. Sort(IComparer)
  3. Sort(Int32, Int32, IComparer)

Sort()

This method is used to Sort the elements in the entire ArrayList. It uses the QuickSort algorithm to sort the elements of the ArrayList.

Note: This method is an O(n log n) operation, where n is Count and in the worst case, it is an O(n^2) operation.

Syntax:

public virtual void Sort ();

Exception: This method will give NotSupportedException if the ArrayList is read-only.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate Sort() Method
using System;
using System.Collections;
  
class GFG {
  
    // Main method
    public static void Main()
    {
  
        // create and initalize new ArrayList
        ArrayList mylist = new ArrayList();
        mylist.Add("Welcome");
        mylist.Add("to");
        mylist.Add("Geeks");
        mylist.Add("for");
        mylist.Add("Geeks");
        mylist.Add("2");
  
        // ArrayList before sorting
        Console.WriteLine("ArrayList before sort:");
        foreach(string i in mylist)
        {
            Console.WriteLine(i);
        }
        Console.WriteLine();
          
          
        Console.WriteLine("ArrayList after sort:");
  
        // sort the ArrayList
        // using Sort() method
        mylist.Sort();
  
        // ArrayList after sort
        foreach(string i in mylist)
        {
            Console.WriteLine(i);
        }
    }
}

chevron_right


Output:

ArrayList before sort:
Welcome
to
Geeks
for
Geeks
2

ArrayList after sort:
2
for
Geeks
Geeks
to
Welcome

Sort(IComparer)

This method is used to sort the elements in the entire ArrayList using the specified comparer. This method is an O(n log n) operation, where n is Count; in the worst case, it is an O(n^2) operation.

Syntax:

public virtual void Sort (IComparer comparer);

Here, IComparer implementation is used when comparing the elements.

Exceptions:

  • NotSupportedException: If the ArrayList is read-only.
  • InvalidOperationException: Due to an error occurred while comparing two elements.
  • ArgumentException: If the null is passed for the comparer and the elements in the list do not implement IComparable.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustarte Sort(IComparer) Method
using System;
using System.Collections;
  
class GFG {
      
    // Calls CaseInsensitiveComparer.Compare
    // with the parameters reversed.
    public class myClass : IComparer {
          
        int IComparer.Compare(Object a, Object b)
        {
            return ((new CaseInsensitiveComparer()).Compare(b, a));
        }
    }
      
    // Main method
    public static void Main()
    {
  
        // create and initalize new ArrayList, i.e. mylist
        ArrayList mylist = new ArrayList();
        mylist.Add("Welcome");
        mylist.Add("to");
        mylist.Add("geeks");
        mylist.Add("for");
        mylist.Add("geeks");
        mylist.Add("2");
  
        IComparer Comp1 = new myClass();
  
        // sort the value of ArrayList
        // using Sort(IComparer) method
        mylist.Sort(Comp1);
  
        foreach(Object ob in mylist)
        {
            Console.WriteLine(ob);
        }
    }
}

chevron_right


Output:

Welcome
to
geeks
geeks
for
2

Sort(Int32, Int32, IComparer)

This method is used to sort the elements in a range of elements in ArrayList using the specified comparor.

Note: This method is an O(n log n) operation, where n is count and in the worst case, it is an O(n^2) operation.



Syntax:

public virtual void Sort (int index, int count, IComparer comparer);

Parameters:

  • index: It the zero-based starting index of the range to sort and the type of this parameter is System.Int32.
  • count: It count the length of the range to sort and the type of this parameter is System.Int32.
  • comparer: It is the IComparer implementation which is used during the comparision of the elements.

Exceptions:

  • NotSupportedException: If the ArrayList is read-only.
  • InvalidOperationException: Due to an error occurred while comparing two elements.
  • ArgumentException: If the index and count do not specify a valid range in the ArrayList.
  • ArgumentOutOfRangeException: If the index is less than zero.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of
// Sort(Int32, Int32, IComparer) Method
using System;
using System.Collections;
  
class GFG {
  
    // Main method
    public static void Main()
    {
          
        // create and initalize new ArrayList
        ArrayList mylist = new ArrayList();
        mylist.Add("Welcome");
        mylist.Add("to");
        mylist.Add("geeks");
        mylist.Add("for");
        mylist.Add("GFG");
        mylist.Add("2");
  
        // sort the value of ArrayList from 0 to 4
        // using Sort( Int32, Int32, IComparer) method
        // here the value of IComparer is null
        mylist.Sort(0, 4, null);
  
        // Display the sorted string
        foreach(string ob in mylist)
        {
            Console.WriteLine(ob);
        }
    }
}

chevron_right


Output:

for
geeks
to
Welcome
GFG
2

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.