Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What determines ascending or descending order in Comparator / Comparable collection class?

I understand we can sort or order the objects, stored in Collection as per our requirement(s).

While I get deep understanding, I am not convinced by the fact that ascending and descending order of arrangement is achieved by (a - b) ->ascending or (b - a) -> descending where "a" and "b" are class members we chose to compare.

Example:

public int compareTo(Student s) {      return this.grade - s.grade; //ascending order      // return s.grade - this.grade; // descending order } 

What is logic behind ordering object elements? how "(this.grade - s.grade)" if positive 1 moves "this.grade" front and puts "s.grade" next in order, why not other way around? Who validates the compare result (+1, -1, 0) and then puts in ascending order or descending order respectively, is there any documentation that describes internal working of this part?

public class Student implements Comparable <Student>{     String name;     int grade;     public Student(String name, int grade) {         this.name = name;         this.grade = grade;     }     public int compareTo(Student s) {          return this.grade - s.grade; //ascending order          // return s.grade - this.grade; // descending order     }     public String toString() {         return this.name + ", " + this.grade;     } } 

Please share, thank you much!


Edit:

I get the Java docs, my question is this:

sort these grades (13, 2)  Case ascending -> return this.grade - s.grade;  picture in my mind:  compare (13, 2) , (13 - 2) > 0 so move 2 to front. result -> 2, 13 ------ Case descending -> return s.grade - this.grade;  picture in my mind:  compare (2, 13) , (2 - 13) < 0 so move 13 to front.  result -> 13, 2 

"How does this happen?" was my original question. I read the docs, still couldn't figure out.

like image 966
David Prun Avatar asked Sep 29 '14 20:09

David Prun


1 Answers

What is logic behind ordering object elements? how "(this.grade - s.grade)" if positive 1 moves "this.grade" front and puts "s.grade" next in order, why not other way around?

Using negative numbers to say "this is less than that", positive numbers to say "this is more than that" and 0 to say "these 2 things are equal" has been in many computer languages for 30+ years.

Who validates the compare result (+1, -1, 0) and then puts in ascending order / descending order respectively, is there any documentation that describes internal working of this part?

There are several internal classes that use the return value to reorder elements in arrays or collections including

Collections.sort() Arrays.sort() TreeSet

EDIT

To answer HOW that works you will have to look at the source code for each of the classes I listed above. Some of them are quite complicated to try to make the sorting as efficient as possible. But in general, it all boils down to code like this:

if( data[i].compareTo(data[j]) > 0 ){    // swap data[i] and  data[j] } 
like image 116
dkatzel Avatar answered Oct 02 '22 01:10

dkatzel