如何按日期和时间对 jsonarray 数据进行排序并放入列表视图适配器

How to sort a jsonarray data by Date & Time and put in list view adapter

我的 jsonArray 数据如下:

  [{"LeadId":4,
   "CoreLeadId":0,
   "CompanyId":7,
   "AccountNo":"5675",
   "ScheduleOn":"2015-05-11T00:00:00"},
  {"LeadId":7,
   "CoreLeadId":2,
   "CompanyId":8,
   "AccountNo":"sample string 4",
   "ScheduleOn":"2015-12-01T15:04:23.217"}]   

我想按日期和时间(ScheduleOn)排序并放入列表视图。在我的下方,我发送了我设置适配器的代码片段。我们可以分类到 listItemService 中吗?请帮助我。

   JSONArray jsonArray = dpsFunctionFlow.getAllServiceDetail("1");
   listItemService = new Gson().fromJson(jsonArray.toString(),
            new TypeToken<List<AppointmentInfoDto>>() {
            }.getType());
    mAdapter = new AdapterAppointment(getActivity(), listItemService);
    listView.setAdapter(mAdapter);

您应该能够使用 Collections.sort(...) 传递一个 Comparator 来比较 2 AppointmentInfoDto 个对象。

Collections.sort(listItemService, new Comparator<AppointmentInfoDto>() {

    @Override public int compare(AppointmentInfoDto l, AppointmentInfoDto r) {
        // Compare l.ScheduleOn and r.ScheduleOn
    }

}
/// Sort JSON By any Key for date  
public static JSONArray sortJsonArray(JSONArray array,final String key,  final boolean  isCase) {  
        List<JSONObject> jsonsList = new ArrayList<JSONObject>();  
        try {  
            for (int i = 0; i < array.length(); i++) {  
                jsonsList.add(array.getJSONObject(i));  
            }  
            Collections.sort(jsonsList, new Comparator<JSONObject>() {  
                @Override  
                public int compare(JSONObject v_1, JSONObject v_2) {  
                    String CompareString1 = "", CompareString2 = "";  
                    try {  
                        CompareString1 = v_1.getString(key); //Key must be   present in JSON  
                        CompareString2 = v_2.getString(key); //Key must be present in JSON  
                    } catch (JSONException ex) {  
                       // Json Excpetion handling  
                    }  
                    return CompareString1.compareTo(CompareString2);  
                }  
            });  
        } catch (JSONException ex) {  
               // Json Excpetion handling  
        }  
        return new JSONArray(jsonsList);  
    }  

// _Sort JSON for any String Key value.........  
public static JSONArray sortJsonArray(JSONArray array,final String key,   final boolean  isCase) {  
        List<JSONObject> jsonsList = new ArrayList<JSONObject>();  
        try {  
            for (int i = 0; i < array.length(); i++) {  
                jsonsList.add(array.getJSONObject(i));  
            }  
            Collections.sort(jsonsList, new Comparator<JSONObject>() {  
                @Override  
                public int compare(JSONObject v_1, JSONObject v_2) {  
                    String CompareString1 = "", CompareString2 = "";  
                    try {    
                        CompareString1 = v_1.getString(key); //Key must be present in JSON  
                        CompareString2 = v_2.getString(key); //Key must be present in JSON  
                    } catch (JSONException ex) {  
                       // Json Excpetion handling  
                    }  
                    return isCase ? CompareString1.compareTo(CompareString2) : CompareString1.compareToIgnoreCase(CompareString2);
                }  
            });  
        } catch (JSONException ex) {  
             // Json Excpetion handling  
        }  
        return new JSONArray(jsonsList);  
    }