compare method

int compare(
  1. List a,
  2. List b
)
override

Returns the first non-zero result of compareTo encountered as the two Collections are iterated over. If, by the time one of the iterations is complete, no non-zero result has been encountered, returns 0 if the other iteration is also complete. If b completes before a, a positive number is returned; if a before b, a negative number.

@param a a Collection of Comparables @param b a Collection of Comparables @return the first non-zero compareTo result, if any; otherwise, zero

Implementation

int compare(List a, List b) {
  Iterator i = a.iterator;
  Iterator j = b.iterator;
  while (i.moveNext() && j.moveNext()) {
    Comparable aElement = i.current as Comparable;
    Comparable bElement = j.current as Comparable;
    int comparison = aElement.compareTo(bElement);
    if (comparison != 0) {
      return comparison;
    }
  }
  if (i.moveNext()) {
    return 1;
  }
  if (j.moveNext()) {
    return -1;
  }
  return 0;
}