Reputation: 321
I know that compare and compareTo
returns an int
value.
For example:
Returns
0 if a equal b
-1 if a < b
+1 if a > b
sort
method makes a call to either compareTo
or compare()
methods. But how does sort
method arranges the list
when compare or compareTo
returns a int
value. What is background scenario running after a compare
or compareTo
returns an int value to sort?how does sort
method makes use of int values(-1
or 0
or 1
) returned to it from compare
and compareTo
Upvotes: 29
Views: 40657
Reputation: 809
Hopefully, these implementations of compare() method would help you understand it better.
public int compare(Object obj1, Object obj2)
{
Integer I1 = (Integer)obj1; // typecasting object type into integer type
Integer I2 = (Integer)obj2; // same as above ..
// 1.
return I1.compareTo(I2); // ascending order [0, 5, 10, 15, 20]
// 2.
return -I1.compareTo(I2); // descending order [20, 15, 10, 5, 0]
// 3.
return I2.compareTo(I1); // descending order [20, 15, 10, 5, 0]
// 4.
return -I2.compareTo(I1); // ascending order [0, 5, 10, 15, 20]
// 5.
return +1; // insertion order [10, 0, 15, 5, 20, 20]
// 6.
return -1; // reverse of insertion order [20, 20, 5, 15, 0, 10]
// 7.
return 0; // only first element [10]
}
How compare() method works in Java
Upvotes: 0
Reputation: 178431
General case sorting algorithms are based on comparisons. If you compare between a
and b
- there are exactly 3 possibilities: a == b
, a > b
, a < b
.
The compare()
or compareTo()
method provides exactly this information.
Now, to use this information we have designed numerous sorting algorithms, starting with naive bubble sort, and some are more advances such as quick sort. Each providing a bit different approach to the sorting problem.
Java chose the TimSort algorithm for its sorting implementation.
As an exercise, you can design your own1 sorting algorithm. Can you find the maximum element of an array using the compare()
method? when you found it where should it be? what should you do next?
(1) Well, think of it by yourself, but it already exists, actually :)
Upvotes: 4
Reputation: 310884
If the two elements (a,b) being compared are already in the right order, compare(a, b)
and a.compareTo(b)
both return a value that is <= 0
, so nothing has to happen.
If they aren't in the right order, the return value is > 0
, indicating that they must be interchanged.
Upvotes: 28
Reputation: 1956
Read this document:
a.compareTo(b)
:
Comparable interface. Compares values and returns an int which tells if the values compare less than, equal, or greater than.
If your class objects have a natural order, implement the Comparable interface and define this method. All Java classes that have a natural ordering implement this (String, Double, BigInteger, ...).
compare(a, b)
:
Comparator interface. Compares values of two objects. This is implemented as part of the Comparator interface, and the typical use is to define one or more small utility classes that implement this, to pass to methods such as sort() or for use by sorting data structures such as TreeMap and TreeSet. You might want to create a Comparator object for the following:
Upvotes: 2