MooN
when 'a' is compared to 'b' if it returns a +ve int, that means a>b, then when 'b' is compared to 'a' it should return -ve int, again that means b<a
Originally posted by jibs parap:
a>b whether a is compared with b or b is compared with a. So the natural order which is an ascending order should be [a, b] irrespective of how its compared.
1. the sorting order is defined according to the int returned to see one object is less/more than compared to the other object.
2. then, the objects are sorted in ascending order
MooN
MooN
the sort() method will call thisObject.compareTo(anotherObject) to sort thisObject and anotherObject. If thisObject.compareTo(anotherObject) implementation returns +ve then anotherObject will come before thisObject and if thisObject.compareTo(anotherObject) implementation returns -ve then thisObject will come before anotherObject.
when 'a' is compared to 'b' if it returns a +ve int, that means a>b, then
when 'b' is compared to 'a' it should return -ve int, again that means b<a
In my statement I was always talking about thisObject.compareTo(anotherObject) or a.compareTo(b) not about the reverse one. Because the Collections.sort() method is using the compareTo() method and handles the order of calling compareTo() method either a with b or b with a.when 'a' is compared to 'b' if it returns a +ve int, that means a>b, then
when 'b' is compared to 'a' it should return -ve int, again that means b<a
MooN
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |