Forums Register Login

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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Forget my last post, and the duplication of my detailed post. My browser had refresh problems.
+Pie Number of slices to send: Send
 

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)
It's feeding time! Give me the food you were going to give to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3121 times.
Similar Threads
sorting..
BinarySearch
Arrays sort doubt.
Error in Sun Certified Programmer for Java 6 Study Guide
Arrays java.util collections class - binarySearch() method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:24:23.