java list按另一list 排序
枚举类
让list 按照枚举顺序排序
public enum DemoEnumTest {
IMPORT_VALUE("价值客户", "1"),
IMPORT_RECALL("唤回客户", "2"),
IMPORT_DEEP_PLOUGHING("深耕客户", "3"),
IMPORT_RETENTION("挽留客户", "4"),;
private final String value;
private final String code;
DemoEnumTest(String value, String code) {
this.value = value;
this.code = code;
}
}
创建的基础对象
@Data
public class BrandRFM {
/**
* 数据类型
*/
private String dataType;
/**
* 数量
*/
private String count;
/**
* 占比
*/
private String pro;
/**
* 品牌/品类
*/
private String type;
}
基础算法换算
public static void main(String[] args) {
List<BrandRFM> brandRFMList = new ArrayList<>();
BrandRFM rfm3 = new BrandRFM();
rfm3.setCount("3465");
rfm3.setDataType("深耕客户");
brandRFMList.add(rfm3);
BrandRFM rfm2 = new BrandRFM();
rfm2.setCount("345");
rfm2.setDataType("唤回客户");
brandRFMList.add(rfm2);
BrandRFM rfm = new BrandRFM();
rfm.setCount("213");
rfm.setDataType("价值客户");
brandRFMList.add(rfm);
BrandRFMEnum[] values1 = BrandRFMEnum.values();
List<String> collect = Arrays.stream(values1).map(str -> {
String stre = str.getValue();
return stre;
}).collect(Collectors.toList());
System.out.println(JSON.toJSONString(collect));
setListOrder(collect,brandRFMList);
System.out.println(brandRFMList);
}
public static void setListOrder(List<String> orderRegulation, List<BrandRFM> targetList) {
// 按照 list 里的 name 来排序 targetList
Collections.sort(targetList, ((o1, o2) -> {
int io1 = orderRegulation.indexOf(o1.getDataType());
int io2 = orderRegulation.indexOf(o2.getDataType());
if (io1 != -1) {
io1 = targetList.size() - io1;
}
if (io2 != -1) {
io2 = targetList.size() - io2;
}
return io2 - io1;
}));
}