How to sort JSONArray in android

后端 未结 3 1921
谎友^
谎友^ 2021-01-23 16:21

How to sort JSONArray by its Tag name with ascending and descending on android.

In my application i have a JSON like the following and need to display the data according

3条回答
  •  攒了一身酷
    2021-01-23 16:51

    Following code may useful to you.

    ArrayList user_array;
    Collections.sort(user_array, new Comparator() {
        @Override
        public int compare(ResultBean data1, ResultBean data2) {
            if (data1.getUserId() >= data2.getUserId()) {
                return -1;
            } else {
                return 1;
            }
        }
    });
    

    Your ResultBean will look like following.

    public class ResultBean {
        //other methods and declarations...
        public int getUserId() {
            return userId;
        }
    }
    

提交回复
热议问题