JSONArray排序


Integer排序

        JSONArray objectArr = new JSONArray();
        objectArr.add(3);
        objectArr.add(2);
        objectArr.add(4);
        System.out.println("end objectArr 开始: "+objectArr.toString());
        objectArr.sort(Comparator.comparing(obj->(Integer)obj));
        System.out.println("end objectArr 正序: "+objectArr.toString());
        objectArr.sort(Comparator.comparing(obj->(Integer)obj).reversed());
        System.out.println("end objectArr 正序: "+objectArr.toString());

 

JSONObject排序

        String str = "[{\"score\":77,\"id\":\"A04\"},{\"score\":88,\"id\":\"A02\"},{\"score\":66,\"id\":\"A01\"},{\"score\":44,\"id\":\"A08\"},{\"score\":99,\"id\":\"A01\"}]";
        JSONArray array = JSON.parseArray(str); System.out.println("begin array: "+array.toString()); //正序 array.sort(Comparator.comparing(st ->((JSONObject)st).getString("score"))); System.out.println("end array 正序: "+array.toString()); //倒序 array.sort(Comparator.comparing(st->((JSONObject)st).getString("score")).reversed()); System.out.println("end array 倒序: "+array.toString());

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM