Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Sorting with a comparator

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Sorting with a comparator

    I am trying to sort an ArrayList of objects with the comparator as I want to sort based on a certain value for each object. I understand I would need to override compareTo() in the objects class, is there any way I can get around also needing to override for all subclasses of the object?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,247
    Thanks
    52
    Thanked 2,299 Times in 2,270 Posts

    Default Re: Sorting with a comparator

    What do you mean when you say "objects"? An ArrayList can only contain objects, not primitives.
    How is the ArrayList defined? What type of object does it hold?
    How can a Comparator compare two objects that are held in the ArrayList? What field in the objects can be compared?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Sorting with a comparator

    The objects hold fields, I want the arraylist to sort the objects based on a certain field.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,247
    Thanks
    52
    Thanked 2,299 Times in 2,270 Posts

    Default Re: Sorting with a comparator

    Yes, most objects hold fields. You need to explain a bit more.
    Can you post an example of a class definition that will be used to create an object that will be held in the ArrayList?
    And say what field in that class will be used to compare objects of that class.

    --- Update ---

    What did you do to solve this problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Sorting with a comparator

    Forgive the late response, just saw your post now, I did the following:

    Collections.sort(myList,
                     new Comparator<Object>()
                     {
                         public int compare(Object one, Object two)
                         {
                             return one.getField().compareTo(two.getField());
                         }        
                     });


    --- Update ---

    An anonymous inner class.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,247
    Thanks
    52
    Thanked 2,299 Times in 2,270 Posts

    Default Re: Sorting with a comparator

    Does that compile? Where is the method: getField() defined in the Object class?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Sorting with a comparator

    It works, it's not the actual source code it's just shows the template I used. getField() refers to any getter method. Object refers to the Object type the list stores.

Similar Threads

  1. Comparator implementation?
    By lorenxvii in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2014, 06:26 AM
  2. Problem with Comparator
    By iHank in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 30th, 2013, 05:41 AM
  3. Comparator interface question
    By brisingrbrom in forum Collections and Generics
    Replies: 3
    Last Post: February 24th, 2012, 09:54 AM
  4. Replies: 0
    Last Post: October 2nd, 2009, 10:51 PM
  5. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM