I have this compareTo
code for my list:
public int compareTo(className a)
{
return (this.long1 > a.long1) ? 1 : -1;
}
When I use Collections.sort(list)
, I get the following error: Comparison method violates its general contract!
When I change it to if (this.long1 >= a.long2)
, it works, but it does not sort properly. The longs are sorted in order, then out of order, then in order. Using >=
, the output looks like this:
...
2000100
2000101
2000102
1000100
1000101
2000101
2000102
...
Now, duplicates do happen, and they need to be sorted correctly. It doesn't matter if the duplicates appear first or last, as long as they're properly grouped in order, like so:
...
2000100
2000101
2000101
2000102
2000102
1000100
1000101
...
How would I do this properly? Thank you.
UPDATE
The list is still being sorted out of order with all of the below suggestions. Is this because it's a List<Class> list = new ArrayList<Class>();
? I can't use what I'm used to from C#: List<Class> list = new List<Class>()
.