java中对List中JavaBean的某个属性进行排序

在我们进行java开发的时候,我们会遇到对List中的某个属性进行排序。

这个时候我们需要重些Collections.sort方法。

Collections.sort(plist, new Comparator<ProductReason>() {
    public int compare(ProductReason o1, ProductReason o2) {
        //按照CreatTime字段进行降序排列
        if (o1.getCreateTime().getTime() < o2.getCreateTime().getTime()) {
            return 1;
        }
        if (o1.getCreateTime().getTime() == o2.getCreateTime().getTime()) {
            return 0;
        }
        return -1;
    }
});

这样返回结果plist就按照createTime降序排列。

猜你喜欢

转载自blog.csdn.net/wszhm123/article/details/89003835