如何在java中根据map键->对象属性对具有映射的对象列表进行排序?



我有一个对象列表(folodto),它看起来像这样

public class FoliosDto implements Serializable {
private static final long serialVersionUID = 5307750871230L;
private Map<String, ActionDto> folioConfig = new HashMap(); 
}

和ActionDto如下所示:

public class ActionDto {
private Boolean isAllowed;
private String remark;
}

和我有List<FolioDto>folio,我想在folioDto.getfolioConfig().isAllowed字段的基础上对folodto进行排序,其中所有的folodto都是isAllowed为真,应该首先出现。

尝试:

Collections.sort(folios, new Comparator<FoliosDto>() {
@Override
public int compare(FoliosDto holding1, FoliosDto holding2) {
Map<String, Object> folioConfig1 = holding1.getFolioConfig();
Map<String, Object> folioConfig2 = holding2.getFolioConfig();
ActionDto schemeActionDto1 = (ActionDto) folioConfig1.get("key1");
SchemeActionDto schemeActionDto2 = (ActionDto) folioConfig2.get("key1"));
return -1 * Boolean.compare(schemeActionDto1.getIsAllowed(), schemeActionDto2.getIsAllowed());
}
});

但是这不起作用,并且不能对对象列表进行排序。

我想不明白,请给我提点建议

我自己盘算了一下。

private void sortFolios(List<HoldingFoliosDto> folios) {
Collections.sort(folios, sortingByIsAllowedRedeem.reversed());
}
public Comparator<HoldingFoliosDto> sortingByIsAllowedRedeem = (dto1, dto2) -> {
Map<String, Object> folioConfig1 = dto1.getFolioConfig();
Map<String, Object> folioConfig2 = dto2.getFolioConfig();
SchemeActionDto schemeActionDto1 = (SchemeActionDto) folioConfig1.get("key1");
SchemeActionDto schemeActionDto2 = (SchemeActionDto) folioConfig2.get("key1");
return Boolean.compare(schemeActionDto1.getIsAllowed(), schemeActionDto2.getIsAllowed());
};

最新更新