Android how to sort JSONArray of JSONObjects

前端 未结 6 1785
一整个雨季
一整个雨季 2020-11-30 09:52

I have made a jsonarray of jsonobjects. Now I need to sort the JSONArray on base of a value from the jsonobjects. Formerly I sorted ArrayLists of custom objects like this:

6条回答
  •  醉梦人生
    2020-11-30 10:18

    If you are going to display the data contained in the JSONArray then it may make sense to sort it in the adapter itself. For example, the ArrayAdapter class already has the requisite methods such as Insert, Remove, and of course Sort.

    adapter.sort(new Comparator(){
    
        @Override
        public int compare(JSONObject arg0, JSONObject arg1) {
    
            return arg0.optString("SortField", "").compareTo(arg1.optString("SortField","")) ;
    
        }
    
    });
    

提交回复
热议问题