Peter DeWeese and others gave you very good answers. you can use
Collections.sort(myList, new MyComparator());
to sort myList using your comparator. & lt == == What does this mean?
In Java, if something implements Comparable (java.lang.comparable) , then you can determine the order for your elements. It looks like you know that Java Generics , since you used them to declare your ArrayList as type <Employee>. This is awesome because you can store an Employee object in every entry in an ArrayList. So far so good?
However, if you want to sort objects, you must first determine the order. Since objects can have different properties, perhaps I want to sort my employees by ear size. In this case, I just tell Java that my class implements Comparable. With generics, I have to point out that it implements Comparable <Employee> because I am defining the order for my Employee objects (peons, minions, whatever).
Peter Davy mentioned:
public int compareTo(Employee e) { return this.getData().getLast().compareTo(e.getData().getLast()); }
and Jason Goemat mentioned:
public int compareTo(Employee other) { return Data.Last.compareTo(other.Data.Last); }
What does it mean? If I say that my class implements Comparable, then I need to define a compareTo function. (An interface is a set of methods that must be implemented). The compareTo function determines the order of my elements.
From the comparative <T> spec:
int compareTo(T o)
Compares this object with the specified order object. Returns a negative integer, zero, or a positive integer since this object is less than, equal to, or greater than the specified object.
If I compare the sizes of the ears and say that I want the big ears to come first on my list, then I could (re) define compareTo as:
public int compareTo(Employee e) { if (this.earSize > e.earSize) //big ears come first return -1; if (this.earSize == e.earSize) //equality return 0; else return 1; // if e.earSize > this.earSize then return 1 }
To answer Steve Guoโs question, we put the this keyword in our comparator, because when we call the compareTo method
x.compareTo(y);
this keyword will refer to x .
You may think that compareTo is a method of the x object, so when you call x.compareTo (y), you really say this.compareTo (y) from the scope of the x object.
We can also see an example line:
This means that if I want โMedvedevโ to appear before โPutinโ (since โMโ comes to โPโ in the English alphabet), I would have to say that I want to compare in order to return -1 when comparing Medvedev with Putin
String TheMString = "Medvedev"; String ThePString = "Putin";
that line
TheMString.compareTo(ThePString);
will be evaluated as -1.
Now a standard procedure, such as Collections.sort ( list , comparator), will be able to use these values, which compareTo returns to determine the [absolute] order of the list . As you know, sorting is a comparison-based operation, and we need to know which value is โlessโ or โmoreโ of another value in order to have a meaningful look.
One big caveat is that if you call compareTo on Strings, the alphabetical order is used by default, so you can just say that compareTo returns A.compareto (B), and it will make sure the strings are ok.
Usually (well, I have to say, in other cases) when overriding the compareTo method, you should explicitly specify the return value neg / zero / pos.
I hope this helps.