Convert long to int, keep positive/negative/0

integer, java, long-integer

Solution

That's easy - `Long` itself provides an implementation:

public int compare(Long l1, Long l2) {
    return l1.compareTo(l2);   
}

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 `long` values and you're using Java 1.7, you can use `Long.compare(long, long)`. Otherwise, go with your current implementation.

Problem

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).

Original source