File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array.sort(ArrayTosort,Comparator)

 
bharat bhasin
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to implement the compare method in the Comparator interfaceOr something similar.

You can usually get away without overriding the equals() method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic