Question

I'm Using Gson Library.I need to sort an JsonArray of JsonObjects based on keys in the JsonObjects.The JsonArray is given below

[{"AS1000":["5.1","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-02 08:07:00.0"},
{"AS1000":["5.0","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-02 17:58:00.0"},
{"AS1000":["5.0","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-30 14:59:00.0"},
{"AS1000":["-","-","-","-","-","-"],"AS2000":["27.0","0","true","false","30.0 - 20.0",null],"Date":"2014-03-30 06:03:00.0"},
{"AS1000":["-","-","-","-","-","-"],"AS2000":["15.0","0","false","false",null,null],"Date":"2014-03-02 02:00:00.0"},
{"AS1000":["5.0","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-14 20:06:00.0"},
{"AS1000":["5.0","0","false","false",null,null],"AS2000":["-","-","-","-","-","-"],"Date":"2014-03-30 07:00:00.0"},
{"AS1000":["-","-","-","-","-","-"],"AS2000":["61.0","0","false","false",null,null],"Date":"2014-03-17 19:07:59.0"}]

Is it possible to sort based on keys in the object for example AS2000[0] or Date?

I have tried Boon like this

        JsonArray listLoggerDataObj=gson.fromJson(listloggerdata, JsonArray.class);
        List<JsonObject> list=new ArrayList<JsonObject>();
         for (JsonElement entry : listLoggerDataObj) {
         list.add(entry.getAsJsonObject());
         }


        sort(list,"Date"); /*list ,key used to sort*/
        System.out.println("printing sorted" +list.toString());

But it prints the same input.Any Ideas?

Thanks,

Gowrish

Was it helpful?

Solution

Found a solution.Used Boon's jsonParser to construct Json from string (not Gson's fromJson()) and then casted into List,finally applied sort(),Issue Fixed.

  Object jsonObject = fromJson(listloggerdata);
  List<?> jsonDepartments = (List<?>) jsonObject;

  sort(jsonDepartments,sortBy("Date"));

  System.out.println("printing sorted" +jsonDepartments.toString());

the sorted result is

[{"AS2000":["15.0","0","false","false",null,null],"AS1000":["-","-","-","-","-","-"],"Date":"2014-03-02 02:00:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.1","0","false","false",null,null],"Date":"2014-03-02 08:07:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.0","0","false","false",null,null],"Date":"2014-03-02 17:58:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.0","0","false","false",null,null],"Date":"2014-03-14 20:06:00.0"},
 {"AS2000":["61.0","0","false","false",null,null],"AS1000":["-","-","-","-","-","-"],"Date":"2014-03-17 19:07:59.0"},
 {"AS2000":["27.0","0","true","false","30.0 - 20.0",null],"AS1000":["-","-","-","-","-","-"],"Date":"2014-03-30 06:03:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.0","0","false","false",null,null],"Date":"2014-03-30 07:00:00.0"},
 {"AS2000":["-","-","-","-","-","-"],"AS1000":["5.0","0","false","false",null,null],"Date":"2014-03-30 14:59:00.0"}]

You can even sort based on array index like this

 sort(jsonDepartments,sortByDesending("AS2000[0]"));

Hope this might help someone.

OTHER TIPS

I have also used Boon library but i am getting error "java.lang.NoClassDefFoundError org.boon.sort.Sort"

Please tell me if i am doing something wrong .

            this.list = global.getPartyPartneruserlistwithstatus().getJSONObject(0).getJSONArray("in_list");


            List<JSONObject> list1 = new ArrayList<JSONObject>();
            for (int i = 0; i < global.getPartyPartneruserlistwithstatus().getJSONObject(0).getJSONArray("in_list").length(); i++) {
                    list1.add(list.getJSONObject(i));
            }

                sort(list1,"age");

and list contains JSONARRAY in

[ { "username": "Navdeep Singh Sandhu", "hometown": "chandigarh", "request_id": "2", "social_id": "100002878462251", "social_provider": "facebook", "age": 25 }, { "username": "HarPreet Kaur", "hometown": "chandigarh", "request_id": "4", "social_id": "100002029425217", "social_provider": "facebook", "age": 23 }]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top