如何用字符串对JSONOBJECT进行排序?

时间:2018-04-06 02:30:22

标签: java android json

我希望解析一个json api来在Android中构建一个列表视图,它完全正常工作但我不能通过" overall_league_position"来排序对象。 json。

我已经看到很多关于此主题的问题,但我不知道为什么我可以在我的代码中实现 Collections.sort 。我想是因为我的json以数组而不是对象开始。

JSON

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

爪哇

 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();
                            }

                        }

                    },

非常感谢你的宝贵帮助,并教我:) PS:我遵循了这个教程: https://www.androidlearning.com/android-json-parsing-using-volley/

1 个答案:

答案 0 :(得分:0)

这里你要对字符串值进行排序,但我认为overall_league_position值总是以整数形式排序,所以你的排序不起作用。

对于此排序,您可以尝试下面的代码

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