CorArray: Sort

Sort

Sorts an entire array. An optionally supplied comparer object can be used to compare special elements, such as userdefined values.



 Public Sub Sort(
	  ByRef Arr As Variant,
	  Optional ByRef Comparer As Variant )

Parameters

Arr
[ByRef] Variant. The array to sort.
Comparer
[ByRef] Optional. Variant. (Optional) An IComparer object or address of a comparer callback function.

Remarks

The Comparer parameter can be an IComparer interface or a callback address to a compare function using the AddressOf method.
Callback Method Signature
Public Function SortCallback(ByRef x As <Type>, ByRef y As <Type>) As Long
    ' return less than zero if x is less than y.
    ' return greater than zero if x is greater than y.
    ' return zero if x equals y.
End Function
The <Type> the type of element of the array. If the array is an array of Variants, then <Type> would be a Variant, not any specific sub-type within the variant elements of the array.

Examples

This example creates an array of unsorted integers then uses the CorArray.Sort method to sort the array, displaying the results.

Private Sub Main()
    Dim ints(9) As Integer
    Dim i As Long
    
    ' Fill array with random values.
    Rnd -13
    For i = 0 To UBound(ints)
        ints(i) = Rnd * 10
    Next i
    
    ' Display original unsorted array of integers.
    Debug.Print "Unsorted Array of Integers."
    DisplayInts ints
    
    ' Sort array of integers.
    CorArray.Sort ints

    ' Display sorted array of integers.
    Debug.Print t("\nSorted Array of Integers.")
    DisplayInts ints
End Sub

' Displays an array of integers.
Private Sub DisplayInts(ByRef ints() As Integer)
    Dim i As Long
    
    For i = LBound(ints) To UBound(ints)
        Debug.Print CorString.Format("ints({0}) = {1}", i, ints(i))
    Next i
End Sub

' This code produces the following output.
'
'  Unsorted Array of Integers.
'  ints(0) = 6
'  ints(1) = 4
'  ints(2) = 0
'  ints(3) = 3
'  ints(4) = 9
'  ints(5) = 7
'  ints(6) = 1
'  ints(7) = 0
'  ints(8) = 4
'  ints(9) = 9
'
'  Sorted Array of Integers.
'  ints(0) = 0
'  ints(1) = 0
'  ints(2) = 1
'  ints(3) = 3
'  ints(4) = 4
'  ints(5) = 4
'  ints(6) = 6
'  ints(7) = 7
'  ints(8) = 9
'  ints(9) = 9

See Also

Project CorLib Overview

Class CorArray Overview

IComparer

SortEx

SortKey

SortKeyEx