1

here my jsonArray data like:

  [{"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"}]   

i want to sort by dateandtime(ScheduleOn) and put into listview. below i side i send snnipt of my code where i set adapter. can we sort into listItemService. Please help me.

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

2 Answers 2

1

You should be able to use Collections.sort(...) passing in a Comparator that will compare 2 AppointmentInfoDto objects.

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

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

}
Sign up to request clarification or add additional context in comments.

Comments

0
/// 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);  
    }  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.