Comparable

Quick overview of the Comparable interface. The idea is that, for things that implement Comparable, there is an ordering of those elements; we can say 'bee' comes before 'eel' by alphabetical ordering for Strings, for instance. And for Integers, 17 comes before 18, etc.

It's defined as interface with one method:

  public interface Comparable<T>
  {
    public int compareTo(T t);
  }

When comparing 2 things (compare a to b by writing a.compareTo(b) ) compareTo should return -1 if a<b, return 1 if a>b, and return 0 if the two are equivalent. Which element is greater than the other depends on what you're looking at; for Strings, it's alphabetical ordering, for numbers it's numerical ordering.

To implement the interface:

class OrderedBlob implements Comparable<OrderedBlob> { 
    public int compareTo(OrderedBlob b) { 
        // needs to return a value that is...
        // <0 if "this" object is less than b, 
        // 0 if this and b are equal
        // >0 if this is greater than b;
    } 
}