Results 1 to 8 of 8
  1. #1
    jeanjiang is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default Comparable and Comparator

    When we want to sort a collection, we can use class Comparable or Comparator.
    My question is what is the difference between them.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    They are similar, googling will answer for the most part.

    Comparator forces you to implement compareTo, which makes the object this(keyword), compare itself to the argument object.

    With comparable, it compares two objects.

    Java Code:
     Comparable enforces
     int 	compareTo(T o)
              Compares this object with the specified object for order.
    
    comparator enforces
     int 	compare(T o1, T o2)
              Compares its two arguments for order.
    Others may have to give more details, perhaps I will learn something, I am having a hard time conveying my thoughts fully.

    http://lmgtfy.com/?q=what+is+the+dif...arator+in+java

    I believe this explanation will illustrate my thoughts.

    Lets say you have a class for peoples addresses, it has names, phone numbers, and addresses. If you would like to sort it by each of these things which would you do?
    You could have the address class implement comparable, the problem is it only allows you to sort one way. Perhaps you could have an inner class that implements comparable? This wouldn't work because it uses this to compare the argument with, the this in an inner class would be the inner class, not the actual class.

    If you use comparators you can have an inner class which takes two objects and lets you compare and sort based on different things.

    In the address example you can have 3 inner classes which all implement comparators. Each of these comparators would sort based on a different instance variable(address, name, number)
    Last edited by sunde887; 04-21-2011 at 06:17 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    From the Collections class:
    Java Code:
    sort(List<T> list) 
    sort(List<T> list, Comparator<? super T> c)
    If you call the first method all the Objects in the List must implement Comparable. If you call the other sort method you must write your own class that implements Comparator and pass an object of that class to the method. They basically do the same thing. It just depends upon your design.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Comparable is for when you want to sort a class by its "natural order", the one and main way it should be sorted. For it to work, the objects being sorted must implement the Comparable interface and have the proper compareTo method. The Comparator is for when the objects being sorted do not implement Comparable and so you must create a separate class that implements Comparator and usually has a single public method, compare.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Also, imagine you have a Collection of people (Person class). If you use the Comparator way to sort, then you can have 2 or more Comparators: one to sort by last name, one to sort by first name, one to sort by address etc. If the Person class implements Comparable then you are stuck with sorting in only one way.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Also, oftentimes you will not be able to rewrite (other people's) classes to make them implement Comparable and that's when writting a Comparator for them is the only way to sort them.

  7. #7
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Comparable :
    It uses the compareTo() method.
    int objectOne.compareTo(objectTwo).
    It is necessary to modify the class whose instance is going to be sorted.
    Only one sort sequence can be created.
    It is frequently used by the API classes.

    Comparator:
    it uses the compare() method.
    int compare(ObjOne, ObjTwo)
    A separate class can be created in order to sort the instances.
    Many sort sequences can be created.
    It used by third-party classes to sort instances.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    There's another difference: an object implementing the Comparable<T> interface can do what it wants because the interface implementation is part of the object. A Comparator<T> implementation can only compare those details from an object that are made visible to the outside world.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. sorting by Comparator
    By Dayanand in forum New To Java
    Replies: 4
    Last Post: 03-11-2011, 11:38 AM
  2. how to use comparator
    By KidneyinaCooler in forum Advanced Java
    Replies: 2
    Last Post: 07-18-2010, 11:25 AM
  3. Got hit by the Comparable block again, help pls.
    By niu_niu in forum New To Java
    Replies: 7
    Last Post: 06-24-2010, 10:49 AM
  4. Comparable Interface
    By Yelrubk in forum New To Java
    Replies: 3
    Last Post: 04-28-2010, 03:46 PM
  5. Using Comparable and Comparator interfaces
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •