File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Comparator explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparator explanation" Watch "Comparator explanation" New topic
Author

Comparator explanation

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

This is not sorting, only providing if these two elements are equal or not, to determine their relative position.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7798
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparator explanation