This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sorting array 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 "sorting array" Watch "sorting array" New topic
Author

sorting array

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
class search {



public static void main(String [] args) {
String [] sa = {"one", "two", "three", "four"};
Arrays.sort(sa); // #1
for(String s : sa)
System.out.print(s + " ");
System.out.println("\none = "+ Arrays.binarySearch(sa,"three")); // #2
System.out.println("now reverse sort");
ReSortComparator rs = new ReSortComparator(); // #3
Arrays.sort(sa,rs);
for(String s : sa)
System.out.print(s + " ");
System.out.println("\none = "+ Arrays.binarySearch(sa,"one")); // #4
System.out.println("one = "
+ Arrays.binarySearch(sa,"one",rs)); // #5
}

static class ReSortComparator
implements Comparator<String> { // #6
public int compare(String a, String b) {
return b.compareTo(a); // #7
}
}
}


In this command the array gets sorted in reverse order after that underlined statement in the code,hows this happen,where did we mention a reverse kind of thing to working on array?in the comparae function we mentioned to return b.compareto(a) value does it responsible for this if yes then how?
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Hi Vijay,
Welcome to JavaRanch..........

Please use code tags wherever code is written. It is very difficult to read without them.

in the comparae function we mentioned to return b.compareto(a) value does it responsible for this if yes then how?

Yes............I guess


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Line 7, determines the sort arrangement.



Usually the implementation of Comparator<String>.compare() will sort the array of strings by their natural order ("a", "b", "c" ... "f"), but here the call to String.compareTo() is using reversed arguments so the order is ("f" .... "c", "b", "a"). Simply replace


with


to the get the same output produced with Arrays.sort(Object[])
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
thank you very much for replying!
ya I am preparing for scjp 1.5

But how can you say that b.CompareTo(a) will sort the array in natural order what is the reason for that?Can you give Explanation behind it?

i have one more doubt about it, that what does the a.compareTo(b) actually returns... when i tried to learn this i read that it return 1,0,-1 while the objects are compared.

I want to know how the objects are compared on what basis the object are compared.. we say that for example: this object is greater than the other object and returns value greater than 0 i...e 1. how that works .. on what basis the two objects are compared wether is it length???

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

But how can you say that b.CompareTo(a) will sort the array in natural order what is the reason for that?Can you give Explanation behind it?


The compareTo() method does *not* sort anything. The sorting is done by the Arrays class -- whose sort() method that you called. This sorting algorithm checks if this element is greater than that, if yes, move it there, if this element is greater than that, move that there... etc. etc. etc.

This is where your compareTo() method comes in. The sorting algorithm don't know if an element is greater than another -- so it calls your compareTo() method to do it. Your method is responsible for comparing only two elements, and it will be called many times during the sorting process.

i have one more doubt about it, that what does the a.compareTo(b) actually returns... when i tried to learn this i read that it return 1,0,-1 while the objects are compared.


Yup, that is what it should return -- a positive number, a zero, or a negative number, depending whether the objects are greater than, equal, or less than.

I want to know how the objects are compared on what basis the object are compared.. we say that for example: this object is greater than the other object and returns value greater than 0 i...e 1. how that works .. on what basis the two objects are compared wether is it length???


How an object is compared is totally up to you. It is an instance of your class. You designed the class. So you should know which object is greater than another.

Henry

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

Joined: Feb 22, 2009
Posts: 192
I said:

Usually the implementation of Comparator<String>.compare() will sort the array of strings by their natural order ("a", "b", "c" ... "f"),


I worded that badly. compare() doesn't sort anything. Arrays.sort() uses compare() (when given a comparator) to test the relationship between two objects. You send Arrays.sort() a comparator when you want the sorting behavior to act differently (in case reversed). Otherwise, the method assumes the array argument is of type Comparable and uses compareTo().

a.compareTo(b) will return 1 if a > b and b.compareTo(a) will return -1 if b > a, hence a reversing comparator

Well the natural order for classes extending Number (you can only call compareTo() on objects) - Integer, Float, Byte, etc. - is obvious (1, 2, 3, 4 ... 1000). For an array of Strings - new String[]{"1", "ba", "3", "d", "bb"} - the sorted array will be

1
3
ba
bb
d

you can intuitively figure that one out. All other classes must implement the Comparable interface and override compareTo() where the implementation is left to the programmer.
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
HI ! THANKS FOR CLEARING THE DOUBT!



THE LAST LINE "How an object is compared is totally up to you. It is an instance of your class. You designed the class. So you should know which object is greater than another."
CAN YOU GIVE A SMALL EXPLANATION ABOUT IT!

I am STILL NOT CLEAR THAT FOR EXAMPLE IF WE WRITE LIKE
aaa.CompareTo(bbb) -->what will it return , wether 1,-1,0?
how will the aaa will be compared to the bbb.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

I am STILL NOT CLEAR THAT FOR EXAMPLE IF WE WRITE LIKE
aaa.CompareTo(bbb) -->what will it return , wether 1,-1,0?
how will the aaa will be compared to the bbb.


It's your code -- you designed it. And if you want your classes to be sortable, you have to have a concept of order. How the actual comparison is to happen, completely depends on what your definition of order is for you class.

For example, let say you have a class that represents a point on a map. And you decided that the natural order should run West to East, with the international dateline as the dividing point. So for that to work, you need some data in the class that gives the longitude. When compareTo() method is called, you check the two longitude values and report if the first one is further West (less than) or futhter East (greater than).

Henry

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
I really like that example Henry


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
So you are telling like we have to define which object will be greater or which object will be smaller?

The CompareTo() method can not decide that on it's own? is it like this?


[EDIT: Please don't shout]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

The CompareTo() method can not decide that on it's own? is it like this?


Comparator and Comparable are interfaces -- so don't know what you mean by "decide that on it's own".

Many of the built-in classes such as String, Integer, Double, etc., all implement this method. And the sorting algorithm calls them. So the sorting algorithm doesn't even know how to sort strings, integers, etc. without calling the data types' compareTo() method. So ... The answer is no. It can't "decide that on it's own".

Henry

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
So a class decides which object is to be greate and smaller? isn't it? and the compareTo() method based on that criteria defined by class returns -1,0, or 1?
 
jQuery in Action, 2nd edition
 
subject: sorting array