0

I have a JSON Array:

public JSONObject toJSONObject(DataJSONConverter dataJsonConverter) {
    JSONObject jsonResponse = new JSONObject();

    if (datas != null && dataJsonConverter != null) {
        JSONArray jsonDatas = new JSONArray();
        // for (Iterator<? extends Object> iter = datas.iterator(); iter.hasNext();) {
        // jsonDatas.put(dataJsonConverter.toJSONObject(iter.next()));
        // }

        for (Object iter : datas) {
            jsonDatas.put(dataJsonConverter.toJSONObject(iter));
        }

        jsonResponse.put("datas", jsonDatas);
        jsonResponse.put("count", count);

    }

Result in "datas" :

    [{"id":60685,"libelle":"BILAN3","typeAnomalie":"Trop de points 
invalides","dateDebut":"18\/07\/2013 00:00","numero":"1268"}

    {"id":60628,"libelle":"_fictif","typeAnomalie":"Trop de points 

invalides","dateDebut":"02\/06\/2013 00:00","numero":"1242"}

    {"id":14672,"libelle":"NAVIL 949","typeAnomalie":"D\u00e9passement","dateDebut":"13\/05\/2012 12:00","numero":"263"}]

How can I have an ascending order by "dateDebut" ?

Thx

7
  • 1
    You should sort your Java objects first, and then convert them to JSON. I'm sure you'll find hundreds of results if you google for "sort objects in Java" Commented Jul 18, 2013 at 13:22
  • ^ As suggested by JB Niznet. Google it. Commented Jul 18, 2013 at 13:25
  • 1
    I googled it for you: stackoverflow.com/questions/5927109/… Commented Jul 18, 2013 at 13:25
  • @JBNizet JSONArray does not extend List or Collection. Commented Jul 18, 2013 at 13:44
  • 1
    Re-read. Sort the Java objects first, THEN transform the list of objects to JSON. Commented Jul 18, 2013 at 13:44

2 Answers 2

1

JSONArrays are not sortable. You can extract the data into an ArrayList and then sort with a comparator and then put back into the JSONArray.

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

2 Comments

I can not extract the data into an ArrayList
Why can't you use remove()?
0

Boon (a Java open source lib) has what is currently the fastest JSON parser on the JVM (circa March 2014) and it provides support for sorting, filtering, searching (with indexes), JSON.

http://www.dzone.com/links/1123623.html (Path Expressions and JSON)

http://www.dzone.com/links/1123621.html (Sorting JSON)

http://www.dzone.com/links/1091051.html (Using the Parser)

http://www.dzone.com/links/java_boon_filtering_for_java_beans_json_and_java.html (Using the Index Search to do JSON ETL and searching and sorting)

http://www.dzone.com/links/java_boon_filtering_for_java_beans_json_and_java.html (Using Boon to filter JSON)

Here is a sorting example:

    Object jsonObject = fromJson(json);
    List<?> jsonDepartments = (List<?>) jsonObject;
    List<?> jsonEmployees = (List<Employee>) atIndex(jsonDepartments, "employees");

    sort(employees); //natural sort


    sort( employees, "lastName"); //sort by last name



    sort( departmentList );



    sort( employees, sortBy( "department.name" ),
                     sortByDescending( "lastName" ),
                     sortBy( "firstName" ) ); //well you get the idea



    sort(employees,
            sortBy("contactInfo.phoneNumbers[0]")); //you can even sort by an path expression




    sort( employees,
            sortByDescending("contactInfo.phoneNumbers[0]") ); //forward and backwards

Oh yeah... one more thing.. It is screaming fast. :)

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.