Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: ArrayList sorting by object's 2 different ints

  1. #1
    endgame is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default ArrayList sorting by object's 2 different ints

    How can i sort my ArrayList, which contains cars, with year and used year, i want to sort them first from year, and then from used year . what should i use?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: ArrayList sorting by object's 2 different ints

    Write a Comparator for the Collections class's sort method.
    Compare the most important first, if they are equal, compare the next one.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    endgame is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: ArrayList sorting by object's 2 different ints

    where do i write the comparator? i dont know, in my class car on in my arraylist class, that has all the arrays?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: ArrayList sorting by object's 2 different ints

    You can make it an inner class near where the sort is to be done.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    endgame is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: ArrayList sorting by object's 2 different ints

    can you give me a example im not following.
    in my Cars i need to override the comparator? or where

  6. #6
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default Re: ArrayList sorting by object's 2 different ints

    Yes, you are right.

    You are pushing car objects into arraylist. Therefore, you do have to implement Comparable interface in your Car class and override compareTo() method.
    Mak
    (Living @ Virtual World)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: ArrayList sorting by object's 2 different ints

    A Comparator is generally preferable unless you can define a pretty solid "natural order" for your class.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: ArrayList sorting by object's 2 different ints

    I also like Comparator because it just adds self-documenting code.

    Java Code:
    List<Car> cars = ...;
    
    Collections.sort(cars); // yeah... how is it sorted?
    
    Collections.sort(cars, new Car.BuildYearUsedYearComparator()); // Aha!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Question about sorting in ArrayList
    By ExX in forum New To Java
    Replies: 1
    Last Post: 02-04-2012, 09:13 PM
  2. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 12:48 PM
  3. Sorting ArrayList by object data
    By drymsza1234 in forum New To Java
    Replies: 2
    Last Post: 04-15-2010, 02:22 AM
  4. Sorting an ArrayList
    By flesh-bound-book in forum New To Java
    Replies: 3
    Last Post: 02-13-2010, 01:20 PM
  5. Replies: 1
    Last Post: 03-04-2009, 07:14 PM

Posting Permissions

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