ArrayList: Sort

Sort

Sorts the items in the entire ArrayList or range of elements in ArrayList.



 Public Sub Sort(
	  Optional ByRef Index As Variant,
	  Optional ByRef Count As Variant,
	  Optional ByVal Comparer As IComparer )

Parameters

Index
[ByRef] Optional. Variant. The zero-based index to start sorting the items.
Count
[ByRef] Optional. Variant. The length of the range to sort.
Comparer
[ByVal] Optional. IComparer. The IComparer implementation to use when comparing elements.
-or-
A Nothing reference to use the default Comparer implementation.

Remarks

If comparer is set to Nothing, this method 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.

Exceptions

ExceptionCondition
ArgumentOutOfRangeException Index is less than zero.
-or-
Count is less than zero.
ArgumentException Index and Count do not specify a valid range in the ArrayList.
InvalidOperationException An error occurred while comparing two elements.

Examples

The following code example shows how to sort the values in a range of elements in an ArrayList using the default comparer and a custom comparer that reverses the sort order.

There are two modules created for this example. A BAS module containing the Main method and a Class module implementing the IComparer interface.

The following is a Visual Basic Class module named MyReverserClass.

Option Explicit
Implements IComparer

Private mComparer As New CaseInsensitiveComparer

Public Function Compare(ByRef a As Variant, ByRef b As Variant) As Long
    Compare = mComparer.Compare(b, a)
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   IComparer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function IComparer_Compare(a As Variant, b As Variant) As Long
    IComparer_Compare = Compare(a, b)
End Function

The following is a Visual Basic BAS module containing Main start-up method.

Public Sub Main()
    Dim List As New ArrayList
    Dim MyComparer As New MyReverserClass
    
    List.Add "The"
    List.Add "QUICK"
    List.Add "BROWN"
    List.Add "FOX"
    List.Add "jumped"
    List.Add "over"
    List.Add "the"
    List.Add "lazy"
    List.Add "dog"
    
    Debug.Print "The ArrayList initially contains the following values:"
    PrintIndexAndValues List
    
    List.Sort 1, 3, Nothing
    Debug.Print "After sorting from index 1 to index 3 with the default comparer:"
    PrintIndexAndValues List
    
    List.Sort 1, 3, MyComparer
    Debug.Print "After sorting from index 1 to index 3 with the reverse case-insensitive comparer:"
    PrintIndexAndValues List
End Sub

Private Sub PrintIndexAndValues(ByVal List As ArrayList)
    Dim i As Long
    
    For i = 0 To List.Count - 1
        Debug.Print CorString.Format(t("\t[{0}]:\t{1}"), i, List(i))
    Next
    
    Debug.Print
End Sub

' This code produces the following output (individual values may vary because they are randomly generate):
'
' The ArrayList initially contains the following values:
'    [0]:    The
'    [1]:    QUICK
'    [2]:    BROWN
'    [3]:    FOX
'    [4]:    jumped
'    [5]:    over
'    [6]:    the
'    [7]:    lazy
'    [8]:    dog
'
' After sorting from index 1 to index 3 with the default comparer:
'    [0]:    The
'    [1]:    BROWN
'    [2]:    FOX
'    [3]:    QUICK
'    [4]:    jumped
'    [5]:    over
'    [6]:    the
'    [7]:    lazy
'    [8]:    dog
'
' After sorting from index 1 to index 3 with the reverse case-insensitive comparer:
'    [0]:    The
'    [1]:    QUICK
'    [2]:    FOX
'    [3]:    BROWN
'    [4]:    jumped
'    [5]:    over
'    [6]:    the
'    [7]:    lazy
'    [8]:    dog

See Also

Project CorLib Overview

Class ArrayList Overview