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

comparable and comparator interface

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
these lines are from k&s book... i could not understand the difference between using comparator and comparable interface....what is there use and how they are used....

The other handy thing about
the Comparator interface is that you can use it to sort instances of any class—even
classes you can't modify—
unlike the Comparable interface, which forces you to
change the class whose instances you want to sort.



please explain with an example!!!
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Vijay..

you need to understand this line in broader way.. let me explain some..



above code will help you to understand your question..

See in Client code i have did sorting 3 time..

one natural using comparable
2nd using comparator
3rd using comparator - reverse order

but if you consider way one.. and i want to change my sorting criteria i must have to change the test class
but if i do same thing using comparator - then i just need to introduce new comparator.. but it`s client side code no need to change original class...



 
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
if in the test class the method use is

public Test(String i) method


and we added
st.add(new Test("hello"));
st.add(new Test("ball"));
st.add(new Test("people"));
st.add(new Test("joy"));


now if we these things instead then how the output manipulated!!! can you please explain how we will get the output instead of only telling what we will get!!!please
 
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
can any one reply to this question
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question is very basic.. it will be very hard to expain all the things..

better you go through K&B Chapter 6 - Topic sorting Collections , Comparable Interface and Comparator interface - page (549 -555). it will give you clear idea how flow goes..

 
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
i have read the k&s bok... but there is no practical implication with the explanation.... So any oone can please explain in brief how the above wil be manipulated if given as an input...
 
author
Posts: 23908
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

vijay umar wrote:So any oone can please explain in brief how the above wil be manipulated if given as an input...




The comparable interfaces does *not* manipulate. It doesn't do any searching. It doesn't do any sorting. etc. The purpose of the comparable interface is to compare two objects (or to compare one object to another). Period.

If you look at all sorting algorithms -- bubble sort, quick sort, heap sort, etc. etc. -- you'll notice that at a very basic level, it depends on the capability to compare two items. So... The compare() method doesn't do any sorting. The sorting is done by the collections.sort() method that you are using. However, when the algorithm (implemented by the method) needs to compare and decide whether it should move an element in the list, it uses the compare method.

Henry
 
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
k..thanks...will contact you for further query!!!
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic