2017-05-06 79 views
0

我有ArrayList有三个值(字符串),但第二个是数字,我想从数字0到例如100.在这个时候,它是1,10,2,21,5等。在输出我想有1,2,5,10,21。如何排序值arraylylist <hashmap <string,string >>

JSONObject jsonObj = new JSONObject(jsonStr); 

       JSONArray devices = jsonObj.getJSONArray("list"); 

       for (int i = 0; i < devices.length(); i++) { 
        JSONObject c = devices.getJSONObject(i); 
        String id = c.getString("id"); 
        String name = c.getString("name"); 
        String known = c.getString("known"); 
        String description = c.getString("description"); 
        String controllerId = c.getString("controllerId"); 


        JSONObject frequencySurvey = c.getJSONObject("frequencySurvey"); 
        if (frequencySurvey.has("5000")) { 
         JSONObject fivezero = frequencySurvey.getJSONObject("5000"); 

         String timestamp = fivezero.getString("timestamp"); 
         String clients = fivezero.getString("clients"); 
         String enabled = fivezero.getString("enabled"); 

         HashMap<String, String> device = new HashMap<>(); 

         device.put("id", id); 
         device.put("name", name); 
         device.put("enabled", enabled); 
         device.put("clients", clients); 

         DevicesList.add(device); 

        } 


ListAdapter adapter = new SimpleAdapter(Devices_5_0.this, DevicesList, 
       R.layout.list_item, new String[]{ "name","clients","enabled"}, 
       new int[]{R.id.name,R.id.clients, enabled}); 

     lv.setAdapter(adapter); 

而且我用这种,但排序字符串:

class SortValues implements Comparator { 
    public int compare(Object obj1, Object obj2) { 
     HashMap<String, String> test1 = (HashMap<String, String>) obj1; 
     HashMap<String, String> test2 = (HashMap<String, String>) obj2; 
     return test1.get("clients").compareTo(test2.get("clients")); 
    } 
} 

回答

0

您可以将数字字符串转换成整数

class SortValues implements Comparator { 
    public int compare(Object obj1, Object obj2) { 
     HashMap<String, String> test1 = (HashMap<String, String>) obj1; 
     HashMap<String, String> test2 = (HashMap<String, String>) obj2; 
     return new Integer(test1.get("clients")) 
      .compareTo(new Integer(test2.get("clients"))); 
    } 
} 
0

如果客户的价值是你的电话号码,这不错:

class SortValues implements Comparator { 
    public int compare(Object obj1, Object obj2) { 
     HashMap<String, String> test1 = (HashMap<String, String>) obj1; 
     HashMap<String, String> test2 = (HashMap<String, String>) obj2; 
     Long firstClients = Long.valueOf(test1.get("clients")); 
     Long secondClients = Long.valueOf(test2.get("clients")); 
     return firstClients.compareTo(secondClients); 
    } 
} 
0

试试这个:

Collections.sort(arrayList,new ArrayListHashMapComparator()); 

public class ArrayListHashMapComparator implements Comparator<HashMap<String,String>> { 

     @Override 
     public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) { 

      try { 
       return lhs.get(lhs.keySet()).compareToIgnoreCase(rhs.get(rhs.keySet())); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       return -1; 
      } 
     } 
    } 

上面的代码是所有排序按升序

0

您可以尝试进行排序ArrayList的值下面的代码HashMap中值。

public ArrayList<NotificationValues> mAllNotificationList = new ArrayList<NotificationValues>(); 
Collections.sort(mAllNotificationList, new Comparator<NotificationValues>() { 
@Override 
public int compare(NotificationValues lhs, NotificationValues rhs) { 
    return lhs.getTimestamp().compareTo(rhs.getTimestamp()); 
}}); 
相关问题