将查出的数据对象按某字段自定义排序

将查出的数据对象按某字段自定义排序

无咎 25 2023-05-01

将查出的数据对象按某字段自定义排序

	List<Long> temp = new ArrayList<>();
	temp.add(5L);
	temp.add(1L);
	temp.add(4L);
        List<Entity> entityListTemp = articleRepository.findAllByIdIn(temp);
	// 要定义一个不变属性才能使用排序
        List<Long> tempList = temp;
        // 自定义比较器
        Comparator<Entity> idComparator = Comparator.comparing(i -> tempList.indexOf(i.getId()));
        // 根据 ID 字段列表排序
        entityListTemp.sort(idComparator);