字符串,字符串>如何HashMap的&LT的ArrayList中的数据进行排序;在日期的基础 [英] How to sort data of ArrayList of hashmap<String , String> on The Basis of Date

查看:121
本文介绍了字符串,字符串>如何HashMap的&LT的ArrayList中的数据进行排序;在日期的基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已排序名的基础上的数据。但是当我去约会的基础上,哈希映射的ArrayList的数据进行排序,我不知道如何解决它。我按名称排序的方法是下面给出。

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;
}   

我从XML解析接收数据。在这里我们得到的字符串格式名称,日期,时间等。

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

推荐答案

字符串转换为日期,然后比较一下

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;
}

这篇关于字符串,字符串&GT;如何HashMap的&LT的ArrayList中的数据进行排序;在日期的基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆