简体   繁体   中英

sort jsonarray of data in descending order

[
{
  "id":"1",
  "created_at":"2019-08-19 02:54:36",
  "updated_at":"2019-09-04 15:00:05"
},
{
  "id":"2",
  "created_at":"2019-08-27 08:59:18",
  "updated_at":"2019-09-04 14:59:14"
},
{
  "id":"4",
  "created_at":"2019-08-29 20:19:54",
  "updated_at":"2019-09-04 14:58:53"
}
]

how do i sort json data according to "created_at" (2019-08-30,2019-08-29) data in descending order and set value to textview in android.

please try this

public static JSONArray sortJsonArray(JSONArray array) {
List<JSONObject> jsons = new ArrayList<JSONObject>();
for (int i = 0; i < array.length(); i++) {
    jsons.add(array.getJSONObject(i));
}
Collections.sort(jsons, new Comparator<JSONObject>() {
    @Override
    public int compare(JSONObject lhs, JSONObject rhs) {
        String lid = lhs.getString("created_at");
        String rid = rhs.getString("created_at");
        // Here you could parse string id to integer and then compare.
        return lid.compareTo(rid);
    }
});
return new JSONArray(jsons);
}

A solution using gson library:

public static void setSortedDate (String json, TextView tv) {
        Type t = new TypeToken<List<DateModel>>(){}.getType();
        Gson gson = new Gson();
        List<DateModel> list = gson.fromJson(json, t);
        Collections.sort(list, new Comparator<DateModel>(){
            @Override
            public int compare (DateModel p1, DateModel p2) {
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                try {
                    Date d1 = df.parse(p1.created_at);
                    Date d2 = df.parse(p2.created_at);
                    return d2.compareTo(d1);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                return 0;
            }
        });
        int i = list.size();
        for(DateModel d: list){
            tv.append(d.created_at);
            if(--i > 0) tv.append(", ");
        }
    }

Model class

public class DateModel {
        String id;
        String created_at;
        String updated_at;
    }

Usage

private static final String JSON = "[ { \"id\": \"1\", \"created_at\": \"2019-08-19 02:54:36\", \"updated_at\": \"2019-09-04 15:00:05\" }, { \"id\": \"2\", \"created_at\": \"2019-08-27 08:59:18\", \"updated_at\": \"2019-09-04 14:59:14\" }, { \"id\": \"4\", \"created_at\": \"2019-08-29 20:19:54\", \"updated_at\": \"2019-09-04 14:58:53\" }, { \"id\": \"5\", \"created_at\": \"2019-08-30 09:31:42\", \"updated_at\": \"2019-09-04 14:58:40\" } ]";

setSortedDate(JSON, tv);

Output

2019-08-30 09:31:42, 2019-08-29 20:19:54, 2019-08-27 08:59:18, 2019-08-19 02:54:36

Update

Here is the replacement of gson with standard java implementation

public static void setSortedDate (String json, TextView tv) {
        List<DateModel> list = getListFromJson(json);
        Collections.sort(list, new DateModelComparator());
        int i = list.size();
        for(DateModel d: list){
            tv.append(d.created_at);
            if(--i > 0) tv.append(", ");
        }
    }

private static List<DateModel> getListFromJson (String json) {
        List<DateModel> list = new LinkedList<>();
        try {
            JSONArray array = new JSONArray(json);
            for(int i=0;i<array.length();i++){
                JSONObject obj = array.getJSONObject(i);
                DateModel dm = new DateModel();
                dm.id = obj.getString("id");
                dm.created_at = obj.getString("created_at");
                dm.updated_at = obj.getString("updated_at");
                list.add(dm);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

DateComparator class

public class DateModelComparator implements Comparator<DateModel> {

        @Override
        public int compare (DateModel p1, DateModel p2) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            try {
                Date d1 = df.parse(p1.created_at);
                Date d2 = df.parse(p2.created_at);
                return d2.compareTo(d1);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        return 0;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM