There are two approaches. Either you can implement a custom Comparator or you can have Idea Implement the Comparable<Idea> interface. But you have to be careful: Since id is of type long, you cannot use subtraction as suggested by the other answers. The compare() and compareTo() methods return an int, but subtracting the ids yields a long. Instead, you'll have to compare the ids using the (less-concise) relational operations (e.g. >, <, ==. Here's how you can accomplish this:
Method 1:
In Java 8: Since Comparator is Functional Interface, you can pass a lambda expression to the ideas.sort() method, as follows:
ideas.sort((i1,i2)->(i1.getId() < i2.getId()) ? -1 : ((i1.getId() == i2.getId()) ? 0 : 1));
In pre-Java 8: If you are using a version prior to Java 8, you use an annonymous Comparator:
ideas.sort(new Comparator<Idea>(){
@Override
public int compare(Idea i1, Idea i2) {
(i1 < i2) ? -1 : ((i1 == i2) ? 0 : 1)
}
});
Method 2:
Have the Idea class implement the Comparable<Idea> interface:
public class Idea implements Comparable<Idea>{
private long id;
public long getId(){
return id;
}
@Override
public int compareTo(Idea o) {
(id < o.id) ? -1 : ((id == o.id) ? 0 : 1);
}
}