So I saw an example on here how to sort an ArrayList using the Comparator interface, so I tried it out. With Strings it worked perfectly, but with one variable that I want to so
You can't call methods on int
s; there's no compareTo
method to call. That would be on the boxed Integer
type, but boxing would be overkill.
If you're on Java 7+, you should write
return Integer.compare(o1.getScore(), o2.getScore());
...otherwise you'll more or less need to write
if (o1.getScore() < o2.getScore()) {
return -1;
} else if (o1.getScore() > o2.getScore()) {
return 1;
} else {
return 0;
}