Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

compare() and compareTo()

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not clear about the difference between the compare() method and compareTo(); method,

what arguments it ttakes?
as far as i studied i have not understood the point that
compareto()--- we must modify the class whose instance we want to sort!! what does this mean?

and compare()---- we build a class seperate from the class whose instatnce we want to sort.! what does this mean?

can please give a good example to implement them?will be very helpful in understanding!
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it. Here's an example to clarify your confusion.

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot ... nice exxample!

thus what i understood from the example is if we use comparator we need to do it with sepaerate class and if we use comparable we have to use in the same class which needs to be sorted? am i correct?

Then which is more advantageous?which should be used?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good example...indeed..!!
 
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

vijay umar wrote:thus what i understood from the example is if we use comparator we need to do it with sepaerate class and if we use comparable we have to use in the same class which needs to be sorted? am i correct?
Then which is more advantageous?which should be used?



Using Comparator<V> gives you more flexibility (i.e: ability to sort the objects of the same class in any number of ways you want).
 
We can walk to school together. And we can both read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic