Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator explanation

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

The Comparator implementation contract, as per the java docs, specifies that the method compare(T o1, T o2) should be used to compare mutually comparable types. Consider the following :

I have an array of integers say Integer I[] = {-2,0,6,9,10}

Now I create a ReverseSort class which says

public int compare (int i1, inti2){
return i2-i1;
}

My query is for the statement that says return i2 -i1. It does sort the elements in descending order. But HOW? What actually happens inside the JVM? What is the algorithm that is followed? How does the JVM come to know how to compare 2 objects ? Can someone please explain with an example?
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not sorting, only providing if these two elements are equal or not, to determine their relative position.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:My query is for the statement that says return i2 -i1. It does sort the elements in descending order. But HOW?

Actually, your example is slightly wrong. Comparators only work with objects, so the implementing method would be:
What actually happens inside the JVM? What is the algorithm that is followed? How does the JVM come to know how to compare 2 objects ? Can someone please explain with an example?

1. Don't worry about the JVM; it's irrelevant.

2. Exactly what you wrote in your method.

3. Because you (or, more likely, the object or method that takes the Comparator) told it how to.

4. Imagine that you have 5 balls of different sizes in front of you. How would you sort them by size?
Chances are that you would simply swap them around until they're in order, right?
OK, now think about what each of those "swaps" is - you take two balls, compare their sizes (probably visually), and if they're in the wrong order, you swap them. Well, computers basically do the same thing, and that's where your Comparator comes in; it compares two "things", and returns a negative number if A is less than B, a positive number if A is greater than B, and 0 if they're "equal", based on whatever order you decide.

There's a little more to the ball analogy, because our eyes allow us to pick out which balls we want to swap - at least if there are only 5 they can; if there were 500, it might be a bit trickier - but many computer sorts are quite sophisticated too.

But it all basically boils down to being able to compare two (and ONLY two) objects at a time.

HIH

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mansukhdeep Thind wrote:My query is for the statement that says return i2 -i1. It does sort the elements in descending order. But HOW?

Actually, your example is slightly wrong. Comparators only work with objects, so the implementing method would be:
What actually happens inside the JVM? What is the algorithm that is followed? How does the JVM come to know how to compare 2 objects ? Can someone please explain with an example?

1. Don't worry about the JVM; it's irrelevant.

2. Exactly what you wrote in your method.

3. Because you (or, more likely, the object or method that takes the Comparator) told it how to.

4. Imagine that you have 5 balls of different sizes in front of you. How would you sort them by size?
Chances are that you would simply swap them around until they're in order, right?
OK, now think about what each of those "swaps" is - you take two balls, compare their sizes (probably visually), and if they're in the wrong order, you swap them. Well, computers basically do the same thing, and that's where your Comparator comes in; it compares two "things", and returns a negative number if A is less than B, a positive number if A is greater than B, and 0 if they're "equal", based on whatever order you decide.

There's a little more to the ball analogy, because our eyes allow us to pick out which balls we want to swap - at least if there are only 5 they can; if there were 500, it might be a bit trickier - but many computer sorts are quite sophisticated too.

But it all basically boils down to being able to compare two (and ONLY two) objects at a time.

HIH

Winston


Thanks man. That was good..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic