Comparing Values by User Algorithm

To compare various types of values, use the Compare method of the IComparer interface. To get an object that compares, depending on the data type of compared values, static properties of the Comparer class are used. If one of the available objects does not suit, a custom object can be implemented. To do this, inherit the class from the IComparer interface, and redetermine the Compare method in this class by implementing a custom algorithm of object comparison in this method.

Class MyComparer: Object, IComparer
    Public Function Compare(x: Variant; y: Variant): Integer;
    Var
        sX, sY: String;
    Begin
        sX := String.Trim(x As String);
        sY := String.Trim(y As String);
        If sX.Length = sy.Length Then
            Return 0
        Elseif sX.Length > sy.Length Then
            Return 1
        Elseif sX.Length < sy.Length Then
            Return - 1
        End If;
    End Function Compare;
End Class MyComparer;

Sub UserProc;
Var
    Comp: MyComparer;
    Result: Integer;
Begin
    Comp := New MyComparer.Create;
    Result := Comp.Compare("Test "" Sample");
End Sub UserProc;

In the specified example, in the redetermined Compare method the compared objects are first cast to string type, then spaces at the beginning and the end are removed, then the objects are compared by value length.

See also:

Examples