• 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

Difference between comparable and comparator

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Can someone please suggest me any good site explaining difference between comparator and comparable interface. Thanks
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may refer here
Difference Between comparable and comparator
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more link here.
 
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two nice links, but I prefer Pushkar Choudhary's link.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too liked Pushkar Choudhary's link
I would like to know when the compare() and toCompare( , ) methods actually get called ? Also would like to know the object values that those methods take. For this program i could due a SOP and find out the object values, but would like to what objects are passed to these methods.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like i would have to look into the "sort" method code under collections class to figure out the logic that is being used here, am I correct ?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jose chiramal wrote:Looks like i would have to look into the "sort" method code under collections class to figure out the logic that is being used here, am I correct ?



You could. Or you could just pick up a book on algorithms, and just skim the code. To answer your question....

Yes, you are correct. How compare() or compareTo() methods are called totally depends on the sorting algorithm being used. For example, a bubble sort will call neighboring elments to determine if they should be swapped. Whereas a quick sort algorithm will do a "divide and conquer" approach. Etc. Etc. Etc.

And instead of looking into source to just see how it is done in java, it may be better to understand the algorithm first (I believe the collections sort uses a combination of quick sort and heap sort as the algorithm). Sorting algorithms are generally covered in an university algorithms course, so there are tons of text books for it.

Henry
 
Campbell Ritchie
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought Collections#sort(java.util.List) uses a kind of recursive merge sort.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I thought Collections#sort(java.util.List) uses a kind of recursive merge sort.




Oops... I meant to say a combination of quick and merge sort -- which I read somewhere years ago, but hasn't confirmed, and don't recall where. So, I guess, never mind me...

Henry
 
Campbell Ritchie
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I can remember, it's merge sort without sorting if the top value of the "left" subarray is "less than" the bottom value of the "right" subarray. That way you get a "stable" sort; quick sort is not "stable".
 
Hey! You're stepping on my hand! Help me tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic