3 added compareNullHelper()
source | link
/**
 Sorts first by make, thenprivate model,int thencompareNullHelper(Object yeara, then color.
 */
@Override
public int compareTo(CarObject thatb) {
    // ensures consistency with equals()
    if (this.equals(that)) { returnsorts 0;nulls }
first
    if (this.makea == null) {
        // sorts nulls first
        if (that.makeb != null) { return -1; }
    } else if (that.makeb == null) {
        // sorts nulls first
        return 1;
    } else {
        // For each test, check and only return a non-zero result
        ret = this.make.compareToIgnoreCasecompareTo(that.makeb);
}

/** Sorts by make, model, year, color. */
@Override
public ifint compareTo(ret !=Car 0that) { return ret; }
      // ensures retconsistency =with this.make.compareToequals(that.make);
        if (ret != 0this.equals(that)) { return ret;0; }
   
  }

  int ret if= compareNullHelper(this.model ==make, nullthat.make) {;
        if (that.modelret != null0) { return -1;ret; }
    } else if (that.model == null) {
        return 1;
    } else {
        ret = compareNullHelper(this.model.compareTo(, that.model);
        if (ret != 0) { return ret; }
    }

    ret = this.year - that.year;
    if (ret != 0) { return ret; }

    if (this.color == null) {
        if (that.color != null) { return -1; }
    } else if (that.color == null) {
        return 1;
    } else {
        ret = compareNullHelper(this.color.compareTo(, that.color);
        if (ret != 0) { return ret; }
    }

    // If you can't tell, return -1 which means that they are in the right
    // order but unequal - ensures consistency with equals()
    return -1;
}
/**
 Sorts first by make, then model, then year, then color.
 */
@Override
public int compareTo(Car that) {
    // ensures consistency with equals()
    if (this.equals(that)) { return 0; }

    if (this.make == null) {
        // sorts nulls first
        if (that.make != null) { return -1; }
    } else if (that.make == null) {
        // sorts nulls first
        return 1;
    } else {
        // For each test, check and only return a non-zero result
        ret = this.make.compareToIgnoreCase(that.make);
        if (ret != 0) { return ret; }
        ret = this.make.compareTo(that.make);
        if (ret != 0) { return ret; }
    }

    if (this.model == null) {
        if (that.model != null) { return -1; }
    } else if (that.model == null) {
        return 1;
    } else {
        ret = this.model.compareTo(that.model);
        if (ret != 0) { return ret; }
    }

    ret = this.year - that.year;
    if (ret != 0) { return ret; }

    if (this.color == null) {
        if (that.color != null) { return -1; }
    } else if (that.color == null) {
        return 1;
    } else {
        ret = this.color.compareTo(that.color);
        if (ret != 0) { return ret; }
    }

    // If you can't tell, return -1 which means that they are in the right
    // order but unequal - ensures consistency with equals()
    return -1;
}
private int compareNullHelper(Object a, Object b) {
    // sorts nulls first
    if (a == null) {
        if (b != null) { return -1; }
    } else if (b == null) { return 1; }
    return a.compareTo(b);
}

/** Sorts by make, model, year, color. */
@Override
public int compareTo(Car that) {
    // ensures consistency with equals()
    if (this.equals(that)) { return 0; }
 
    int ret = compareNullHelper(this.make, that.make);
    if (ret != 0) { return ret; }

    ret = compareNullHelper(this.model, that.model);
    if (ret != 0) { return ret; }

    ret = this.year - that.year;
    if (ret != 0) { return ret; }

    ret = compareNullHelper(this.color, that.color);
    if (ret != 0) { return ret; }

    // If you can't tell, return -1 which means that they are in the right
    // order but unequal - ensures consistency with equals()
    return -1;
}
2 Added complete compareTo() implemention for Car
source | link

Implement java.lang.Comparable and use collections like java.util.TreeSet that rely on "natural ordering." Implementing the compareTo() method in the Comparable interface is a lot like implementing an equals() method - that link contains an example of both. It is critical to implement equals() and hashcode() if you implement compareTo() and all three must be compatible with each other! Now

/**
 Sorts first by make, then model, then year, then color.
 */
@Override
public int compareTo(Car that) {
    // ensures consistency with equals()
    if (this.equals(that)) { return 0; }

    if (this.make == null) {
        // sorts nulls first
        if (that.make != null) { return -1; }
    } else if (that.make == null) {
        // sorts nulls first
        return 1;
    } else {
        // For each test, check and only return a non-zero result
        ret = this.make.compareToIgnoreCase(that.make);
        if (ret != 0) { return ret; }
        ret = this.make.compareTo(that.make);
        if (ret != 0) { return ret; }
    }

    if (this.model == null) {
        if (that.model != null) { return -1; }
    } else if (that.model == null) {
        return 1;
    } else {
        ret = this.model.compareTo(that.model);
        if (ret != 0) { return ret; }
    }

    ret = this.year - that.year;
    if (ret != 0) { return ret; }

    if (this.color == null) {
        if (that.color != null) { return -1; }
    } else if (that.color == null) {
        return 1;
    } else {
        ret = this.color.compareTo(that.color);
        if (ret != 0) { return ret; }
    }

    // If you can't tell, return -1 which means that they are in the right
    // order but unequal - ensures consistency with equals()
    return -1;
}

Now you can write:

Implement java.lang.Comparable and use collections like java.util.TreeSet that rely on "natural ordering." Implementing the compareTo() method in the Comparable interface is a lot like implementing an equals() method - that link contains an example of both. It is critical to implement equals() and hashcode() if you implement compareTo() Now you can write:

Implement java.lang.Comparable and use collections like java.util.TreeSet that rely on "natural ordering." Implementing the compareTo() method in the Comparable interface is a lot like implementing an equals() method - that link contains an example of both. It is critical to implement equals() and hashcode() if you implement compareTo() and all three must be compatible with each other!

/**
 Sorts first by make, then model, then year, then color.
 */
@Override
public int compareTo(Car that) {
    // ensures consistency with equals()
    if (this.equals(that)) { return 0; }

    if (this.make == null) {
        // sorts nulls first
        if (that.make != null) { return -1; }
    } else if (that.make == null) {
        // sorts nulls first
        return 1;
    } else {
        // For each test, check and only return a non-zero result
        ret = this.make.compareToIgnoreCase(that.make);
        if (ret != 0) { return ret; }
        ret = this.make.compareTo(that.make);
        if (ret != 0) { return ret; }
    }

    if (this.model == null) {
        if (that.model != null) { return -1; }
    } else if (that.model == null) {
        return 1;
    } else {
        ret = this.model.compareTo(that.model);
        if (ret != 0) { return ret; }
    }

    ret = this.year - that.year;
    if (ret != 0) { return ret; }

    if (this.color == null) {
        if (that.color != null) { return -1; }
    } else if (that.color == null) {
        return 1;
    } else {
        ret = this.color.compareTo(that.color);
        if (ret != 0) { return ret; }
    }

    // If you can't tell, return -1 which means that they are in the right
    // order but unequal - ensures consistency with equals()
    return -1;
}

Now you can write:

1
source | link

Implement java.lang.Comparable and use collections like java.util.TreeSet that rely on "natural ordering." Implementing the compareTo() method in the Comparable interface is a lot like implementing an equals() method - that link contains an example of both. It is critical to implement equals() and hashcode() if you implement compareTo() Now you can write:

Set<Car> orderedCars = new TreeSet<Car>();
orderedCars.addAll(myCars);

If you need to sort according to different criteria, make additional Comparator classes (that's another Java API interface) for each sort order. If you make Car implement Comparable and the compareTo() method of your Car class goes by make, model, year, color, you might want to make a ByColorComparator implements Comparator that compares by color, year, make, model. Then say:

Set<Car> carsByColor = new TreeSet<Car>(new ByColorComparator());
orderedCars.addAll(myCars);

The built-in sorting in Java collections is insanely fast. For years Java collections have been one of the best things about the platform.

One thing to note is that this technique will work with Hibernate or JPA (if you have an open database session) to automatically grab related objects from the database that may be needed for sorting.