How to sort JSONOBJECT by a string?

I’m looking to parse a json api to build a listview in Android, all it’s working perfectly but i can’t sort object by the “overall_league_position” of the json.

I’ve seen many questions about this topic but i don’t know why i can’t implement Collections.sort in my code. I thought because my json starts with an Array and not an object.

JSON

   [
    {
    "country_name": "France",
    "league_id": "129",
    "league_name": "National",
    "team_name": "Grenoble",
    "overall_league_position": "4",
    "overall_league_payed": "26",
...
    },
...
]

Java

 private void loadList() {
            final String JSON_URL = "*************************";
            //creating a string request to send request to the url
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, JSON_URL, null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            //hiding the progressbar after completion and showing list view
                            progressBar.setVisibility(View.GONE);
                            listView.setVisibility(View.VISIBLE);

                            // Showing json data in log monitor
                            Log.d("json", response.toString());

                            try {
                                //we have the array named hero inside the object
                                //so here we are getting that json array


                                //now looping through all the elements of the json array
                                for (int i = 0; i < response.length(); i++) {
                                    //getting the json object of the particular index inside the array
                                    JSONObject jsonObject = response.getJSONObject(i);

                                    //creating a hero object and giving them the values from json object
                                    Movie item = new Movie();


                                    item.setName(jsonObject.getString("team_name"));
                                    item.setPosition(jsonObject.getString("overall_league_position"));
                                    item.setPoints(jsonObject.getString("overall_league_PTS"));



                                    //adding the json data to list
                                    movieList.add(item);



Collections.sort(movieList, new Comparator<movieList>() {
    @Override
    public int compare(movieList lhs, movieList rhs) {
        return lhs.getPosition().compareTo(rhs.getPosition());
    }
});

                                }


                                //creating custom adapter object
                                classementadapter mAdapter = new classementadapter(movieList, getApplicationContext());

                                //adding the adapter to list view
                                listView.setAdapter(mAdapter);


                                mAdapter.notifyDataSetChanged();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                    },

Thank you very much for your precious help, and for teaching me 🙂
PS: I followed this tutorial:
https://www.androidlearning.com/android-json-parsing-using-volley/

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

here you are sorting the string value but i think overall_league_position value always get in integer so your sorting is not working. for this sorting you can try below code Collections.sort(movieList, new Comparator<Movie>() { @Override public int compare(Movie lhs, Movie rhs) { return Integer.parseInt(lhs.getPosition()) - Integer.parseInt((rhs.getPosition())); } });