0

I have a JSONArray called events. I need these events to be sorted by a field called "EVENT_TIME" in descending order. I store the events into an ArrayList called all_events and use Collections.sort() on it with my own comparator. Out of 6 events , 5 get sorted and 1 is in the wrong place.

I end up with:

Acceptance 31-May-2014 10:31:00
Reopened 31-May-2014 10:30:55
Arrived at Pickup Location 31-May-2014 10:30:45
Unloaded from Vessel 31-May-2014 10:30:00
Shipment Create 29-May-2014 00:00:00
Arrive Load Port 30-May-2014 00:00:00

after sorting. Clearly the last one is in the wrong place.

Storing into arraylist and calling the sort:

ArrayList<String>all_events=new ArrayList<String>();
for(int i=0;i<events.length();i++){
    JSONObject event=events.getJSONObject(i);
    all_events.add(event.toString());
}
Collections.sort(all_events, new JSONComparator());;

The JSONComparator class:

class JSONComparator implements Comparator<String>{

        public int compare(String a, String b){
            String a_time=null;
            String b_time=null;
            try{
                JSONObject event1=new JSONObject(a);
                JSONObject event2=new JSONObject(b);
                a_time=event1.getString("EVENT_TIME");
                b_time=event2.getString("EVENT_TIME");
            }
            catch(JSONException e){
                e.printStackTrace();
            }
            SimpleDateFormat sdf=new SimpleDateFormat("ss-MMM-yyyy HH:mm:sss");
            Date date1=null, date2=null;
            try{
                date1=sdf.parse(a_time);
                date2=sdf.parse(b_time);
            }
            catch(ParseException pe){
                pe.printStackTrace();
            }

            return date1.compareTo(date2)*-1;

        }

    };

printing the output:

for(int i=0;i<all_events.size();i++){
    JSONObject event=new JSONObject(all_events.get(i));
    String s=event.getString("EVENT_CODE");
    String eventname=codemap.getString(s);
    System.out.println(eventname+" "+event.getString("EVENT_TIME"));
}
5
  • 1
    Why on earth do you convert your JSON objects to Strings and back again? Also, can't you use a better library than org.json? Commented Jun 2, 2014 at 8:13
  • 1
    Shouldn't ss-MMM-yyyy HH:mm:sss be dd-MMM-yyyy HH:mm:ss? Commented Jun 2, 2014 at 8:13
  • 1
    I agree with fge, it's computationally expensive to parse two strings into JSONObject for each compareTo... Better store the JSONObject in the list, and write a Comparator<JSONObject> Commented Jun 2, 2014 at 8:18
  • @Nizil I tried that first but it gave me some error saying that Collections.sort() couldn't be called on JSONObject... then I changed JSONObject to String and it didnt give any errors, so I went with converting to string and back again, very expensive I know :( Commented Jun 2, 2014 at 8:45
  • @Kartik_Koro Stringe... using the Gson library (or another one) with your own POJO should work :) (moreover, I think Gson is more user-friendly) Commented Jun 2, 2014 at 8:50

1 Answer 1

2

ss-MMM-yyyy HH:mm:sss should be dd-MMM-yyyy HH:mm:ss.

Check out this link and for each letter's meaning in SimpleDateFormat check out Oracle's documentation

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

1 Comment

Thanks, I don't know what I was thinking when I made such a silly error, I guess I just mistyped.

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.