I want to implement a java.util.Comparator with Long:
new Comparator<Long>() {
public int compare(Long l1, Long l2) {
// (*)
}
}
I have a solution with operator ?::
return l1==l2 ? 0 : (l1>l2 ? 1 : -1);
But I wonder if there is any other way to implement it.
(I was trying return (int)(l1-l2), but it’s incorrect).
That’s easy –
Longitself provides an implementation:On the other hand, at that point I’m not sure why you’ve got a custom comparator at all…
EDIT: If you’re actually comparing
longvalues and you’re using Java 1.7, you can useLong.compare(long, long). Otherwise, go with your current implementation.