Progress.Collections.IComparable<T> interface

The Progress.Collections.IComparable<T> interface imposes an ordering on the objects of the class that implements it. Objects that implement this interface can be used as elements in a SortedSet, without the need to specify a custom comparer. The interface provides a method that is used to determine the order of a given element in relation to another element, for the purpose of sorting the elements. By implementing this interface and conforming to its contract, the object provides the natural sort order of the elements in the collection, that is, the natural ordering of the class.

Public Properties

The interface has no public properties.

Public Methods

CompareTo( ) method (Collections)

Example

The following is an example of a class named Person, where the natural order is defined by the value of a property called Name:
CLASS Person IMPLEMENTS Progress.Collections.IComparable<Person>: 

  define property Name as char Get. Set.

  method public integer CompareTo(otherObject as Person).

    if this-object = otherObject OR 
      this-object:Name = otherObject:Name then
      return 0.

    else if this-object:Name < otherObject:Name then
      return -1. // current object sorts lower

    // current object sorts higher
    return 1.
  end.
end.

Notes

  • The following rules apply to the returned value of CompareTo().
    • If the returned value is less than 0 (zero), then the current object sorts before the object passed in.
    • If the returned value equals 0 (zero), then the current object occupies the same position in the sort order.
    • If the returned value is greater than 0 (zero), then the current object sorts after the object passed in.
  • The returned value itself has no meaning outside of being greater than, equal to, or less than zero. That is, if the returned value is 1, or 10, or 10000, they all mean the same thing as per the rules above.
  • The class implementing the interface may implement additional restrictions, such as raising an error if two objects are not comparable.
  • The CompareTo() method should not return the unknown value (?). The SortedSet collection implementation raises an error if the CompareTo() method returns the unknown value (?). It also raises an error if the CompareTo() method raises an error.
  • In order to implement this interface, the user-defined class that wants to be “comparable” needs to define the type argument as a specific class type (usually the same type as the class).

See also

<T> Generic type reference, Progress.Collections.IComparer<T> interface