private void sortChildren ( JSONObject root ) throws JSONException { JSONArray children = root . getJSONArray ( "children" ) ; List < JSONObject > childrenSorted = new ArrayList < JSONObject > ( children . length ( ) ) ; for ( int i = 0 ; i < children . length ( ) ; i ++ ) { childrenSorted . add ( children . getJSONObject ( i ) ) ; } Collections . sort ( childrenSorted , new Comparator < Object > ( ) { @ Override public int compare ( Object o1 , Object o2 ) { int o1IdxLeft = 0 ; int o1IdxRight = 0 ; int o2IdxLeft = 0 ; int o2IdxRight = 0 ; try { o1IdxLeft = ( ( JSONObject ) o1 ) . getJSONObject ( "data" ) . getInt ( SENTENCE_LEFT ) ; o1IdxRight = ( ( JSONObject ) o1 ) . getJSONObject ( "data" ) . getInt ( SENTENCE_RIGHT ) ; o2IdxLeft = ( ( JSONObject ) o2 ) . getJSONObject ( "data" ) . getInt ( SENTENCE_LEFT ) ; o2IdxRight = ( ( JSONObject ) o2 ) . getJSONObject ( "data" ) . getInt ( SENTENCE_RIGHT ) ; } catch ( JSONException ex ) { log . error ( "Could not compare sentence indizes." , ex ) ; } if ( o1IdxLeft + o1IdxRight > o2IdxLeft + o2IdxRight ) { return 1 ; } if ( o1IdxLeft + o1IdxRight == o2IdxLeft + o2IdxRight ) { return 0 ; } else { return - 1 ; } } } ) ; children = new JSONArray ( childrenSorted ) ; root . put ( "children" , children ) ; }
Sorts the children of root by the the sentence indizes . Since the sentence indizes are based on the token indizes some sentences have no sentences indizes because sometimes token nodes are out of context .