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:
- Sort()
- Sort(IComparer)
- 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:
C#
// C# program to illustrate Sort() Method using System; using System.Collections; class GFG { // Main method public static void Main() { // create and initialize new ArrayList ArrayList mylist = new ArrayList(); mylist.Add( "Welcome" ); mylist.Add( "to" ); mylist.Add( "Beginner" ); mylist.Add( "for" ); mylist.Add( "Beginner" ); 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); } } } |
Output:
ArrayList before sort: Welcome to Beginner for Beginner 2 ArrayList after sort: 2 for Beginner Beginner 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:
C#
// C# program to illustrate 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 initialize new ArrayList, i.e. mylist ArrayList mylist = new ArrayList(); mylist.Add( "Welcome" ); mylist.Add( "to" ); mylist.Add( "Beginner" ); mylist.Add( "for" ); mylist.Add( "Beginner" ); 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); } } } |
Output:
Welcome to Beginner Beginner for 2
Sort(Int32, Int32, IComparer)
This method is used to sort the elements in a range of elements in ArrayList using the specified comparer.
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 comparison 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:
C#
// 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 initialize new ArrayList ArrayList mylist = new ArrayList(); mylist.Add( "Welcome" ); mylist.Add( "to" ); mylist.Add( "Beginner" ); 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); } } } |
Output:
for Beginner to Welcome GFG 2
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.sort?view=netframework-4.7.2
Contact Us