简体   繁体   English

如何按字符串对JSONOBJECT排序?

[英]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. 我正在寻找一种解析json api来在Android中建立一个listview的方法,它运行良好,但是我无法按json的“ overall_league_position”对对象进行排序。

I've seen many questions about this topic but i don't know why i can't implement Collections.sort in my code. 我已经看到了很多与此主题有关的问题,但是我不知道为什么我不能在我的代码中实现Collections.sort I thought because my json starts with an Array and not an object. 我以为是因为我的json以数组而不是对象开头。

JSON 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/ 非常感谢您的宝贵帮助,并教了我:) PS:我遵循了本教程: https : //www.androidlearning.com/android-json-parsing-using-volley/

here you are sorting the string value but i think overall_league_position value always get in integer so your sorting is not working. 在这里,您正在对字符串值进行排序,但我认为total_league_position值始终为整数,因此您的排序不起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM