hashmap<String,String>的ArrayList的数据如何排序基于日期 [英] How to sort data of ArrayList of hashmap<String , String> on The Basis of Date
本文介绍了hashmap<String,String>的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;
}
这篇关于hashmap<String,String>的ArrayList的数据如何排序基于日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文