简体   繁体   English

如何根据java中的多个字段值对列表进行排序

[英]How to sort list based on multiple fields value in java

I have list of object type of EventCustomDTO.我有 EventCustomDTO 的对象类型列表。 In this DTO there are many field is there.在这个 DTO 中有很多领域。 I want to sort based on there value.我想根据那里的价值进行排序。

I want sort list based on this three field value.我想要基于这三个字段值的排序列表。 1.passport = true, 2.featured=true, and status of cancel and Unconfirmed are added into last of list. 1.passport = true,2.featured=true,取消状态和未确认状态添加到列表的最后。

@ToString
@AllArgsConstructor
@NoArgsConstructor
@Data
public class EventCustomDTO {
      private Long id;
      private String eventName;
      private Boolean passport;
      private Boolean featured;
      private String status;
}

1.I have try below code 1.我试过下面的代码

Lisi<EventCustomDTO> list = eventRepo.getAllEvents();

list.sort(Comparator.comparing(EventCustomDTO::getPassport).reversed()
.thenComparing(EventCustomDTO::getFeatured)
.thenComparing(EventCustomDTO::getEventStatus));

but it is not working as per my requirement.但它不符合我的要求。 So anyone have idea about it.所以任何人都知道它。 How to sort list of data based on property value.如何根据属性值对数据列表进行排序。

The thenComparing is for nested sorting: when there are objects with equal values, they are then internally sorted by the next property. thenComparing用于嵌套排序:当存在具有相等值的对象时,它们将按照 next 属性在内部进行排序。

What you apparently want is to define your own completely separate sort order:您显然想要的是定义您自己的完全独立的排序顺序:

list.sort(Comparator.comparing(o -> {
    if (Boolean.TRUE.equals(o.getPassport()) {
        return 1;
    } else if (Boolean.FALSE.equals(o.getFeatured()) {
        return 2;
    } else if ("Cancelled".equals(o.getStatus()) {
        return 3;
    } else if ("Unconfirmed".equals(o.getStatus()) {
        return 4;
    } else {
        return 5;
    }
}));

You could let EventCustomDTO implement Comparable and implement your own logic based on your wished fields.您可以让EventCustomDTO实现Comparable并根据您希望的字段实现您自己的逻辑。

@ToString
@AllArgsConstructor
@NoArgsConstructor
@Data
public class EventCustomDTO implements Comparable<EventCustomDTO>{
    private Long id;
    private String eventName;
    private Boolean passport;
    private Boolean featured;
    private String status;

    @Override
    public int compareTo(EventCustomDTO o) {
        return 0;
    }
}

You can return negative values if the object shall be sorted in front of the parameter object.如果对象应排在参数对象的前面,您可以返回负值。

Collections.sort(list, (e1, e2) -> {
    if (e1.getPassport() != e2.getPassport()) {
        return e1.getPassport() ? -1 : 1;
    }

    if (e1.getFeatured() != e2.getFeatured()) {
        return e1.getFeatured() ? -1 : 1;
    }

    if (e1.getEventStatus() != e2.getEventStatus()) {
        // Or do you mean the other way around for this one?
        // Then it would be return e1.getEventStatus() ? 1 : -1;
        return e1.getEventStatus() ? -1 : 1;
    }

    return 0;
});

You mean something like this?你的意思是这样的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM