This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Comparable and Comparator

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can anybody explain me in detail regarding how to work with the comparable and comparator in collections?



Thanks All
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nik,

1- Comparable interface is implemented my classes like String, Integer, Double... Date etc. Comparable enables the objects of the class to be stored in the TreeSet or TreeMap like collection. There should be any way the collection could decide how to place the objects of that class in sorted order that we tell in the compare(...) method of the Comparable interface.

2- Comparator is useful when you need more that one sorting sequence; because you can only create one sorting sequence using Comparable; use can define various classes that implement Comparator interface and define the method compareTo() that takes two arguments.

You can be little specific to your question.

Thank God it is raining here!

Regards,
[ May 01, 2007: Message edited by: Chandra Bhatt ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,
My doubt is how can we create multiple sorting sequence using comparator and only one sorting sequence using comparable. Can you explain me with one example.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when an class implements comparable you give that class a 'natural ordering'. And it wouldn't make sense for a class to have more than one natural ordering.

eg
list.add(new Integer(2));
list.add(new Integer(3));
list.add(new Integer(1));
Collections.sort(list);

the list is now in this order 1,2,3 this is the natural order of Integer.

to get other orderings eg reverse order you can define a comparator and use that.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nik,

See the code below:

 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks chandra and louis
 
I do some of my very best work in water. Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic