IComparer: Compare

Compare

Compares two values and returns a value indicating whether one is less than, equal to, or greater than the other.



 Public Function Compare(
	  ByRef a As Variant,
	  ByRef b As Variant ) As Long

Parameters

a
[ByRef] Variant. The first value to compare.
b
[ByRef] Variant. The second value to compare.

Return Values

Long -  A signed integer that indicates the relative values of a and b, as shown in the following table.
ValueMeaning
Less than zeroa is less than b.
Zeroa equals b.
Greater than zeroa is greater than b.

Remarks

Comparing Null with any type is allowed and does not generate an exception when using IComparable. When sorting, Null is considered to be less than any other value.

Exceptions

ExceptionCondition
ArgumentException Neither a nor b implements the IComparable interface.
-or-
a and b are of different types and cannot be compared.

Examples

The following code example demonstrates the use of the IComparer interface to sort an ArrayList object. In this example, the IComparer interface is implemented using the CaseInsensitiveComparer class to reverse the order of the contents of the ArrayList.

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
    
    List.Add "The"
    List.Add "quick"
    List.Add "brown"
    List.Add "fox"
    List.Add "jumps"
    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
    Debug.Print "After sorting with the default comparer:"
    PrintIndexAndValues List
    
    List.Sort Comparer:=New MyReverserClass
    Debug.Print "After sorting 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.
'
'  The ArrayList initially contains the following values:
'      [0]:    The
'      [1]:    quick
'      [2]:    brown
'      [3]:    fox
'      [4]:    jumps
'      [5]:    over
'      [6]:    the
'      [7]:    lazy
'      [8]:    dog
'
'  After sorting with the default comparer:
'      [0]:    brown
'      [1]:    dog
'      [2]:    fox
'      [3]:    jumps
'      [4]:    lazy
'      [5]:    over
'      [6]:    quick
'      [7]:    the
'      [8]:    The
'
'  After sorting with the reverse case-insensitive comparer:
'      [0]:    the
'      [1]:    The
'      [2]:    quick
'      [3]:    over
'      [4]:    lazy
'      [5]:    jumps
'      [6]:    fox
'      [7]:    dog
'      [8]:    brown

See Also

Project CorLib Overview

Class IComparer Overview

IComparable