• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Array.sort(ArrayTosort,Comparator)

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic