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#
using System;
using System.Collections;
class GFG {
public static void Main()
{
ArrayList mylist = new ArrayList();
mylist.Add( "Welcome" );
mylist.Add( "to" );
mylist.Add( "Geeks" );
mylist.Add( "for" );
mylist.Add( "Geeks" );
mylist.Add( "2" );
Console.WriteLine( "ArrayList before sort:" );
foreach ( string i in mylist)
{
Console.WriteLine(i);
}
Console.WriteLine();
Console.WriteLine( "ArrayList after sort:" );
mylist.Sort();
foreach ( string i in mylist)
{
Console.WriteLine(i);
}
}
}
|
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:
C#
using System;
using System.Collections;
class GFG {
public class myClass : IComparer {
int IComparer.Compare(Object a, Object b)
{
return (( new CaseInsensitiveComparer()).Compare(b, a));
}
}
public static void Main()
{
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();
mylist.Sort(Comp1);
foreach (Object ob in mylist)
{
Console.WriteLine(ob);
}
}
}
|
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 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#
using System;
using System.Collections;
class GFG {
public static void Main()
{
ArrayList mylist = new ArrayList();
mylist.Add( "Welcome" );
mylist.Add( "to" );
mylist.Add( "geeks" );
mylist.Add( "for" );
mylist.Add( "GFG" );
mylist.Add( "2" );
mylist.Sort(0, 4, null );
foreach ( string ob in mylist)
{
Console.WriteLine(ob);
}
}
}
|
Output:
for
geeks
to
Welcome
GFG
2
Reference:
Last Updated :
09 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...