简体   繁体   English

如何对hashmap的ArrayList数据进行排序 <String , String> 在日期的基础上

[英]How to sort data of ArrayList of hashmap<String , String> on The Basis of Date

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. 但是当我要根据“日期”对哈希映射的arraylist数据进行排序时,我不知道如何解决它。 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. 我收到了xml解析的数据。 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;
}

Here example for sort HashMap based on string date 这里是基于字符串日期排序HashMap的示例

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对Arraylist进行排序<Hashmap<String,String> &gt; - How to sort Arraylist<Hashmap<String,String>> 如何排序“ ArrayList <HashMap<String, String> &gt; arrList”按字母顺序排列? - How to sort “ ArrayList<HashMap<String, String>> arrList ” alphabatically? 排序ArrayList <HashMap<String, String> &gt;使用价值 - sort ArrayList<HashMap<String, String>> using value 排序ArrayList <HashMap<String,String> &gt;基于日期时间 - Sort ArrayList<HashMap<String,String>> based on datetime 将数据加载到哈希图 <String, HashMap<String, HashMap<String,ArrayList<ClassOb> &gt;&gt;&gt; - loading data to hashmap<String, HashMap<String, HashMap<String,ArrayList<ClassOb>>>> 如何对ArrayList进行排序 <String> 包含ArrayList的元素 <HashMap<String, String> &gt;已由比较器排序? - How to sort ArrayList<String> containing elements of ArrayList<HashMap<String, String>> that has been sorted by a comparator? 如何在HashMap中对数组列表进行排序 <Integer, ArrayList<String> &gt; - how to sort the array list in HashMap<Integer, ArrayList<String>> 我们如何将数据值添加到哈希图中 <String, ArrayList<String> &gt; - How can we add data values to hashmap<String, ArrayList<String>> 从HashMap检索数据 <String, ArrayList<HashMap<String, String> &gt;&gt; - Retrieve data from HashMap<String, ArrayList<HashMap<String, String>>> 如何存储一个ArrayList <HashMap<String, String> &gt;具有单个Hashmap和多个Arraylist - How to store an a ArrayList<HashMap<String, String>> with a single Hashmap and multiple Arraylist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM