• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Comparable and Comaparator

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
While using the Collections.sort method,I know that one-arg sort method takes only the list whose items are comparable.But,does the two arg sort method also requires that
the list items must be comparable.But then,if it is so,what about this text in K&B.

Comparable:
You must modify the class whose
instances you want to sort.

Comparator:
You build a class separate from the class whose instances you
want to sort.

If the two-arg sort method also takes a comparable list as its arguement,again we will have to modify the class whose objects we wish to compare,but what if we don't have access to the class?

Please pardon if I've put up a silly question and help me out.

Thanks...
 
Rancher
Posts: 3287
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the two-arg sort method also takes a comparable



It takes a Comparator, not a Comparable. They are not the same thing.

There is no need to modify the class when using a Comparator.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,i am sorry,it takes a comparator.
This "modification" of class,what does it mean?Does it mean the modification of the compareTo() method to perform different kinds of sorting ?and do the list argument contents needs to be comparable?
 
Rob Camick
Rancher
Posts: 3287
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read the Comparator API? Where does is talk about the compareTo() method?

Did you search the forum (or the web) for examples that use a Comparator?
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may read the object ordering section of the Java tutorial.
 
Well THAT's new! Comfort me, reliable tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic