• 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

Difference between Comparable vs comparator?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between comparable and comparator? Suppose if you want to sort an object based on two criterias( say name, id) based upon user input, how do we implement it? which one should be used? which is better?
 
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this thread and this one. See whether they help. Ask again if there is something you don’t understand.

If you have a class with dateOfBirth fields, lastName and firstName, sorting on date of birth, then first name, then last name, with a stable sorting algorithm will get your list into this sort of order:-
Ritchie, Campbell 12-3-45
Ritchie, Campbell 23-4-56
Ritchie, Carol 1-1-89
Ritter, Andrew 11-1-42
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guna sekar wrote:what is the difference between comparable and comparator? Suppose if you want to sort an object based on two criterias( say name, id) based upon user input, how do we implement it? which one should be used? which is better?


First: It's Comparable and Comparator. You're in for a lot of frustration unless you remember that Java is case-sensitive.

Second: You don't sort an object, you sort objects. An object may, however, have an order, which is where Comparator and Comparable come in.

Third: What do you mean by "based on user input"? Do you mean that the user gets to choose at runtime how the objects are sorted? If so, you might want to create some sort of ComboComparator<T> class that accepts a set of Comparator<T>'s and applies them in sequence.

I've actually created a class called Index, which does exactly that, along with a class called NaturalOrder that encapsulates the natural order of a Comparable class as a Comparator. Both very useful.

Winston
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic