wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need help with Arrays.sort() and compareTo() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need help with Arrays.sort() and compareTo()" Watch "need help with Arrays.sort() and compareTo()" New topic
Author

need help with Arrays.sort() and compareTo()

sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
Hello folks,

i have a question about (K&B) chapter 7 question 16.

here the code:



the output:

pen
marble
map
key
-1

Actually, i do not understand the compareTo-method in combination with Arrays.sort().
How is it possible to reverse the natural order with compareTo by switching the parameters (like in the example above).

Can anyone explain me step by step how comparTo() in combination with Arrays.sort() is reversing the natural order?


Power from within.

Failed SCJP 2 times :-(
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
you are right, a.compareTo(b) will be in natural order but here its b.compareTo(a) and it is comparing second argument with first and thus reversing the order.


SCJP 5.0 -- 97%
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Mumtaz Khan wrote: you are right, a.compareTo(b) will be in natural order but here its b.compareTo(a) and it is comparing second argument with first and thus reversing the order.


Hi,
I need help with the same question but I need explaination about the binary search. How does it work in general and specifically to this question? Can someone give me a simple breakdown or a few links that can explain it in more details, thanks in advance


SCJP 6.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

Fritz Guerilus wrote:
I need help with the same question but I need explaination about the binary search. How does it work in general and specifically to this question? Can someone give me a simple breakdown or a few links that can explain it in more details, thanks in advance


When used in a binary search, the comparable / comparator are used to determine whether to search up or search down. Keep in mind that for the binary search to work, the array elements must already be sorted, and must be sorted with the same comparing mechanism as with the search.

You can't use one comparator to sort and a different one to search.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
sebastian tortschanoff wrote:Hello folks,

i have a question about (K&B) chapter 7 question 16.

here the code:



the output:

pen
marble
map
key
-1

Actually, i do not understand the compareTo-method in combination with Arrays.sort().
How is it possible to reverse the natural order with compareTo by switching the parameters (like in the example above).

Can anyone explain me step by step how comparTo() in combination with Arrays.sort() is reversing the natural order?


You have to understand the way Comparators work with the sort() method.

The sort() method here is the overloaded version which allows you include a Comparator in the argument.

Arrays.sort(s,o); means you are sorting the Array reference variable-s by means of the Othello reference variable-o of which the class has implemented the Comparator Interface. Remember your IS-A relationship. Since Othello implements Comparator, it IS-A a Comparator.

The Comparator allows you to specify a different way of sorting a collection other than the default
The Comparator interface has a method named compare().
Remember the interface contract---the first concrete class that implements the interface must also implement all of its methods.

Basically the Arrays.sort(s,o); is saying--"Please sort this array according to the instructions in the Comparator, rather than the defualt way, which is usually natural order".

It gets a little trickier next.
Remember, since Arrays.sort(s,o) is sorting Strings, the generitic type of the Comparator must match: "static class Othello implements Comparator<String>"

Inside the class Othello, the compare() method holds the details/instructions on how you want the collection to be sorted.

Since you're sorting Strings, the code is using a shortcut, by invoking the compareTo() method in the String class which does the sorting for you-- a.compareTo(b) puts things in natural order--alphabetically; b.compareTo(a) does the reverse.

Sure you could create your own instructions on how you want to do this, but its already been done for us by means of the String class' compareTo() method.

I'm not sure of the details of how the compareTo() method in the String class works, but right now I don't need to, plus if I did I would just look it up in the API.
All I need to know right now is the result, which is:--it does the sorting for me.

Summary:
I'm using the overloaded sort() method, which takes a Comparator. The Comparator is using the compare() method, which holds the instructions on how I want to sort the 1st argument in the the overloaded sort() method.

Now if you were sorting a Dog Objects instead of Strings, make sure the Dog class implements Comparable/Comparator, and it has instance/member variables that can be used for sorting, or else you'll run into exceptions at runtime.

If any of my info was incorrect or vague, I'm sure the experts here in the forum can definely correct me at provide a clearer explaination. I'm sure I would also learn something. That is why we're all here.
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
sebastian tortschanoff wrote:Hello folks,

i have a question about (K&B) chapter 7 question 16.

here the code:



the output:

pen
marble
map
key
-1

Actually, i do not understand the compareTo-method in combination with Arrays.sort().
How is it possible to reverse the natural order with compareTo by switching the parameters (like in the example above).

Can anyone explain me step by step how comparTo() in combination with Arrays.sort() is reversing the natural order?


You have to understand the way Comparators work with the sort() method.

The sort() method here is the overloaded version which allows you include a Comparator in the argument.

Arrays.sort(s,o); means you are sorting the Array reference variable-s by means of the Othello reference variable-o of which the class has implemented the Comparator Interface. Remember your IS-A relationship. Since Othello implements Comparator, it IS-A a Comparator.

The Comparator allows you to specify a different way of sorting a collection other than the default
The Comparator interface has a method named compare().
Remember the interface contract---the first concrete class that implements the interface must also implement all of its methods.

Basically the Arrays.sort(s,o); is saying--"Please sort this array according to the instructions in the Comparator, rather than the defualt way, which is usually natural order".

It gets a little trickier next.
Remember, since Arrays.sort(s,o) is sorting Strings, the generitic type of the Comparator must match: "static class Othello implements Comparator<String>"

Inside the class Othello, the compare() method holds the details/instructions on how you want the collection to be sorted.

Since you're sorting Strings, the code is using a shortcut, by invoking the compareTo() method in the String class which does the sorting for you-- a.compareTo(b) puts things in natural order--alphabetically; b.compareTo(a) does the reverse.

Sure you could create your own instructions on how you want to do this, but its already been done for us by means of the String class' compareTo() method.

I'm not sure of the details of how the compareTo() method in the String class works, but right now I don't need to, plus if I did I would just look it up in the API.
All I need to know right now is the result, which is:--it does the sorting for me.

Summary:
I'm using the overloaded sort() method, which takes a Comparator. The Comparator is using the compare() method, which holds the instructions on how I want to sort the 1st argument in the the overloaded sort() method.

Now if you were sorting a Dog Objects instead of Strings, make sure the Dog class implements Comparable/Comparator, and it has instance/member variables that can be used for sorting, or else you'll run into exceptions at runtime.

If any of my info was incorrect or vague, I'm sure the experts here in the forum can definely correct me at provide a clearer explaination. I'm sure I would also learn something. That is why we're all here.
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
sebastian tortschanoff wrote:Hello folks,

i have a question about (K&B) chapter 7 question 16.

here the code:



the output:

pen
marble
map
key
-1

Actually, i do not understand the compareTo-method in combination with Arrays.sort().
How is it possible to reverse the natural order with compareTo by switching the parameters (like in the example above).

Can anyone explain me step by step how comparTo() in combination with Arrays.sort() is reversing the natural order?


I can give you the step by step breakdown. I'll post it soon.
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Forget my last post, and the duplication of my detailed post. My browser had refresh problems.
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Henry Wong wrote:
Fritz Guerilus wrote:
I need help with the same question but I need explaination about the binary search. How does it work in general and specifically to this question? Can someone give me a simple breakdown or a few links that can explain it in more details, thanks in advance


When used in a binary search, the comparable / comparator are used to determine whether to search up or search down. Keep in mind that for the binary search to work, the array elements must already be sorted, and must be sorted with the same comparing mechanism as with the search.

You can't use one comparator to sort and a different one to search.

Henry

Thanks. Do you know where can I get more information about binary searches? (specifically, other than doing a googel search or something)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help with Arrays.sort() and compareTo()
 
Similar Threads
sorting..
Arrays java.util collections class - binarySearch() method
BinarySearch
Arrays sort doubt.
Error in Sun Certified Programmer for Java 6 Study Guide