C# | Sort the elements in the ArrayList
Last Updated :
09 Jul, 2021
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("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);
}
}
}
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#
// 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("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);
}
}
}
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#
// 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("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);
}
}
}
Output:
for
geeks
to
Welcome
GFG
2
Reference:
Similar Reads
C# | Remove all elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Clear method is used to remove all the elements from the ArrayLis
3 min read
C# | Remove a range of elements from the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.RemoveRange(Int32, Int32) method is used to remove a range of ele
3 min read
C# | Insert an element into the ArrayList at the specified index ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayLis
3 min read
C# | Copying the SortedList elements to an Array Object SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat
2 min read
C# | Add an object to the end of the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Add(Object) method adds an object to the end of the ArrayList. Pr
2 min read