我已经根据名称对数据进行了排序。但是当我要根据“日期”对散列映射的数组列表的数据进行排序时,我不知道如何解决它。下面给出了我按名称排序的方法。
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 解析的数据。我们以字符串格式获取名称、日期、时间等。
最佳答案
将字符串转换为日期然后比较
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;
}
关于java - 如何根据日期对 hashmap<String , String> 的 ArrayList 的数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22812349/