• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting array

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really like that example Henry
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic