其他分享
首页 > 其他分享> > JSONArray排序

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

 

标签:objectArr,JSONArray,System,score,toString,array,排序,out
来源: https://www.cnblogs.com/zhougongjin/p/15846533.html