1

I'm facing problem while trying to sort an ArrayList of custom object. In fact, after the sorting, nothing has change in my ArrayList. Is something wrong with my method?

Here's my Artist custom object property :

public class Artist {

String mNickname;
String mName;
String mDescription;
String mScene;
String mDay;
String mTime;
String mImageURL;   
Date mDate;

// getters and setters below like getDate() for mDate...

And here's the method use to sort :

static public ArrayList<Artist> sortArrayByDate(ArrayList<Artist> list) {       

    Collections.sort(list, new Comparator<Artist>() {
        @Override
        public int compare(Artist lhs, Artist rhs) {
            if (lhs.getDate().getTime() < rhs.getDate().getTime())
                return -1;
            else if (lhs.getDate().getTime() == rhs.getDate().getTime())
                return 0;
            else
                return 1;
        }
    });

    return list;
}

I know this topic as been discuss many time on StackOverflow, but I can't find why I'm not able to make it work properly. Thanks for your understanding

EDIT : Dates (java.util.date) are create using SimpleDateFormatter

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CANADA_FRENCH);
5
  • could you tell me what kind of date format you used? Commented Jul 16, 2014 at 0:10
  • I construct my date using : SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CANADA_FRENCH); Commented Jul 16, 2014 at 0:22
  • can you post it up in your code? so i can post my answer too :) Commented Jul 16, 2014 at 0:22
  • thank you. so you just use Date class and parse it with this format? Commented Jul 16, 2014 at 0:35
  • I have diffuclty to come up with a format? Commented Jul 16, 2014 at 0:35

1 Answer 1

3

Why not simply use Date#compareTo() for the comparison since java.util.Date implements the Comparable interface.

It is not necessary for the method to return an instance of the List after it is sorted because the underlying List object will be modified by invoking sort. Basically, the method is invoked by passing a reference value as an argument. So when modifications are made, the changes are reflected on the underlying object pointed to by the reference value. With this approach, the code simply passes the List into the method and then continues using the same reference in the proceeding code, which will point to an underlying List which has been sorted.

Another item to consider is modifying the method to accept an argument of the type Listas opposed to ArrayList, since List is an interface, hence more abstract. This would allow the code to switch the implementation of List being passed to the method. This is important because ArrayList does not guarantee the order of the items in the list is maintained. To guarantee the order of items in the List is maintained used LinkedList.

static public void sortArrayByDate(List<Artist> list) {       

    Collections.sort(list, new Comparator<Artist>() {
        @Override
        public int compare(Artist lhs, Artist rhs) {
            return lhs.getDate().compareTo(rhs.getDate());
        }
    });

}

Here is a GitHub Gist I created, that show this method in action, with a complete working example.

Sign up to request clarification or add additional context in comments.

9 Comments

@KickButtowski I'm assuming java.util.Date
just for my learning goal, how your answer will help his issue when he uses this format? SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CANADA_FRENCH);
It shouldn't matter how he constructs the Date once the Date is constructed its a Date and it can be compared using its compareTo method.
I know that but he wants getTime comparison not date?
@KickButtowski getTime returns the date representation of millis since Unix epoch, which is what Date.compareTo does under the covers. You can verify by checking out the Java source. Hooking this into Eclipse is very useful so you can see how anything in the java or javax packages are implemented
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.