Sorts the elements in the entire ArrayList using the specified comparer.
- comparer
- The IComparer implementation to use when comparing elements.
Type Reason InvalidCastException comparer is null, and one or more elements in the current instance do not implement the IComparable interface. NotSupportedException The current instance is read-only. InvalidOperationException comparer is null, and one or more elements in the current instance do not implement the IComparable interface.
Use the ArrayList.Sort(IComparer) method to sort a list of objects with a custom comparer that implements the IComparer interface. If you pass null for comparer, this method uses the IComparable implementation of each element. In this case, you must make sure that the objects contained in the list implement the IComparer interface or an exception will occur.
In addition, using the IComparable implementation means the list performs a comparison sort (also called an unstable sort); that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal. To perform a stable sort, you must implement a custom IComparer interface.
On average, this method is an O(n log n) operation, where n is ArrayList.Count; in the worst case it is an O(n^2) operation.