The compareTo
method is used to establish a natural ordering among the elements of a class. Here is a link which expands the concept of natural ordering.
https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
To get back to your example, I think you're having troubles in understanding the idea of it because you're comparing two String
objects which are in turn invoking their compareTo
implementation which does other operations under the hood. Basically, they just perform an alphabetical comparison to establish if the current string is lower, equal or greater than the given parameter.
Let's make a simpler example with another class, though. Let's say we have a Person
class with just name, last name and age, and that we want to establish its natural ordering by comparing people by their age.
class Person implements Comparable<Person> {
private String name, lastName;
private int age;
public Person(String name, String lastName, int age) {
this.name = name;
this.lastName = lastName;
this.age = age;
}
public String getName() {
return name;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Person otherPerson) {
//If the current person's age is lower than otherPerson's age than we return -1, as the current person is "lower" than the other (ordering-wise)
if (age < otherPerson.getAge()) {
return -1;
}
//If the current person's age is equal to the otherPerson's age than we return 0, as the current person is "equal" to the other (ordering-wise)
if (age == otherPerson.getAge()) {
return 0;
}
//Here, there are no other cases left so the current person's age is greater than the otherPerson's age, so we return 1.
return 1;
}
public static void main(String[] args) {
Person p1 = new Person("Matt", "O'Brien", 20);
Person p2 = new Person("Matt", "O'Brien", 25);
Person p3 = new Person("George", "Lucas", 25);
System.out.println(p1.compareTo(p2)); //Prints -1 because 20 is lower than 25
System.out.println(p2.compareTo(p1)); //Prints 1 because 25 is greater than 20
System.out.println(p2.compareTo(p2)); //Prints 0 because 20 is equal to 20 even though we're comparing different people, because in the comprateTo we're only confronting the age
}
}
What you're doing in your compareTo
example is basically establishing a natural ordering between Spaceship
elements, first by their class and then by their number. In fact, if the alphabetical order of their class is different from 0, then this value is immediately returned. Otherwise, if their class is the same, another comparison is performed (by registration number) and its value returned.
public int compareTo(Spaceship other) {
//Saving the comparison value between the spaceships' class
int spaceshipClassComparison =
this.spaceshipClass.compareTo(other.spaceshipClass);
//If they are already different by class we return the ordering value (no point in comparing also the number)
if(spaceshipClassComparison != 0) {
return spaceshipClassComparison;
}
//If the spaceships have the same class then we return the comparison between their registration number
return this.registrationNo.compareTo(other.registrationNo);
}