cArray: 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 Keys As Variant,
	  Optional ByVal Comparer As Variant )

Parameters

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

Remarks

The Comparer parameter can be an IComparer object or a callback address to a compare function using the AddressOf method. The callback method signature is defined as follows:

 Public Function SortCallback(ByRef x As [Array Datatype], ByRef y As [Array Datatype]) As Long
     ' return a negative value if x is less than y.
     ' return a positive value if x is greater than y.
     ' return 0 if x equals y.
 End Function
 
The [Array Datatype] must be replaced with the datatype of the array. If the array is an array of Variants, then [Array Datatype] would be a Variant, not any specific sub-type within the variants of the array.

Example

' This example creates an array of unsorted integers'' then uses the cArray.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.    Console.WriteLine "Unsorted Array of Integers."    DisplayInts ints        '' Sort array of integers.    cArray.Sort ints    '' Display sorted array of integers.    Console.WriteLine    Console.WriteLine "Sorted Array of Integers."    DisplayInts ints        '' Wait for user to press enter.    Console.ReadLine    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)        Console.WriteLine "ints({0}) = {1}", i, ints(i)    Next iEnd 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 VBCorLib Overview Class cArray Overview cArray Properties cArray Methods Reverse SortEx