2

i have sorted the data on the basis of name. but when i am going to sort the data of arraylist of hash map on the basis of "date", i have no idea how to solve it. my Method for sort by name is given below.

protected ArrayList<HashMap<String, String>> setListOrderByName(ArrayList<HashMap<String, String>> menuItems2) {

    Collections.sort(menuItems2, new Comparator<HashMap<String, String>>() {
        public int compare(HashMap<String, String> mapping1,
                HashMap<String, String> mapping2) {
            return mapping1.get(KEY_NAME).compareTo(mapping2.get(KEY_NAME));
        }
    });

    return menuItems2;
}   

i have receive the data from xml parsing. where we get name, date ,time and etc in string format.

6
  • You don't have a "date" there... Just a List of Map<String, String>. Commented Apr 2, 2014 at 13:03
  • is your date in String format Commented Apr 2, 2014 at 13:04
  • If it is a date stored as string, easiest is to convert string to date and then compare it. Commented Apr 2, 2014 at 13:04
  • 1
    @AmitChotaliya Or store it yyyyMMdd so that it sorts lexically. Commented Apr 2, 2014 at 13:05
  • Here's how to compare date stackoverflow.com/questions/2592501/compare-dates-in-java and parse date stackoverflow.com/questions/999172/how-to-parse-a-date Commented Apr 2, 2014 at 13:06

2 Answers 2

6

convert string to date and then compare it

  protected ArrayList<HashMap<String, String>>      
setListOrderByName(ArrayList<HashMap<String, String>> menuItems2) {

Collections.sort(menuItems2, new Comparator<HashMap<String, String>>() {
DateFormat f = new SimpleDateFormat("dd/MM/yyyy '@'hh:mm a");//do determ    

@Override
public int compare(HashMap<String, String> mapping1,
    HashMap<String, String> mapping2) {
try {
    return f.parse(mapping1.get(KEY_NAME)).compareTo(f.parse(mapping2.get(KEY_NAME)));
} catch (ParseException e) {
    throw new IllegalArgumentException(e);
}
}
});
return menuItem2;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's a very specific date format. How did you determine it?
@ElliottFrisch to determine the String format visit stackoverflow.com/questions/7579227/…
2

Here example for sort HashMap based on string date

    protected ArrayList<HashMap<String, String>> setListOrderByName(ArrayList<HashMap<String, String>> menuItems2) {

        Collections.sort(menuItems2, new Comparator<HashMap<String, String>>() {
            public int compare(HashMap<String, String> mapping1,
                    HashMap<String, String> mapping2) {

                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
                Date date1 = formatter.parse(mapping1.get(DATE));


                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
                Date date2 = formatter.parse(mapping2.get(DATE));

                if(date1.after(date2)){
              return mapping1.get(DATE);
                }else{
                  return mapping2.get(DATE);
                }                           
            }
        });

        return menuItems2;
    } 

Comments

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.