Reputation: 1477
I want to sort a Java list consisting of elements of a class that implements the Comparable interface and has its own compareTo method. What happens when two objects are "equal" (compareTo() returns 0) when compared. Which one ends up before the other in the list?
Upvotes: 0
Views: 1732
Reputation: 40500
Java's sort
implementation is guaranteed to be stable. That means, that objects, that compare as equal will appear in the same order in the sorted lost as they were in the original.
Upvotes: 1
Reputation: 4403
Javadoc says: This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
Upvotes: 1
Reputation: 43391
The docs for Collections.sort
* specify that it's a stable sort, which means that in the event of a tie, the elements stay in the same relative order:
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
* both overloads
Upvotes: 5