java集合排序,多字段排序,集合截取


volist是集合名
集合示例【{名字、数量、年龄;},
{名字、数量、年龄;},...】
需求:
1、根据数量排序
2、数量相等的时候根据年龄排序。

        Collections.sort(voList, (o1, o2) -> {
            int diff = Integer.parseInt(o2.getCount()) - Integer.parseInt(o1.getCount());
            if (diff != 0) {return diff > 0 ? 1 : -1;}
            int diff2 = Integer.parseInt(o2.getAge()) - Integer.parseInt(o1.getAge());
            return diff2 > 0 ? 1 : -1;

        });
        //如果集合超过10条,截取前10条
        if (voList.size()>10) {
            voList = voList.subList(0, 10);
        }
            return voList;