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 Array.sort(ArrayTosort,Comparator) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array.sort(ArrayTosort,Comparator)" Watch "Array.sort(ArrayTosort,Comparator)" New topic
Author

Array.sort(ArrayTosort,Comparator)

bharat bhasin
Greenhorn

Joined: Jul 19, 2009
Posts: 19
In the K&B example of Arrays.sort(ArrayTosort,Comparator) [Page 577 of SCJP 1.6 book, Chapter no. 7 same as in SCJP 1.5 book]
what value does ReSortComaprator classes object rs take when its declared by

rs = new ReSortComparator().

Its used with Arrays.sort( ), what role does the comparator play here??
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The comparator executes the sort logic that you define.

As the sort method goes through your Array, it compares two objects using your Comparator instance. Depending on the result, the objects are shifted or not. This generates the sorted Array.


JDBCSupport - An easy to use, light-weight JDBC framework -
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
You have to implement the compare method in the Comparator interfaceOr something similar.

You can usually get away without overriding the equals() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array.sort(ArrayTosort,Comparator)
 
Similar Threads
need help
doubts regarding java.util.Arrays
Arrays : Help Needed
Comparator's compare() method question
Price of SCJP books