当前位置: 首页 > 知识库问答 >
问题:

按索引数组排序类的列表[重复]

况景龙
2023-03-14

我有长ID的数组列表

Arrays.asList(12, 34, 54, 22, 33);

我有一个具有ownerclass(字符串描述,长id)的对象;

共有1个答案

督嘉言
2023-03-14

这行得通

 final var integers = Arrays.asList(12, 34, 54, 22, 33);

 final var ownerClasses = Arrays.asList(new OwnerClass("", 33), new OwnerClass("", 54), new OwnerClass("", 34), new OwnerClass("", 22), new OwnerClass("", 12));

 ownerClasses.sort(Comparator.comparing(ownerClass -> {var index = integers.indexOf(ownerClass.id); return index != -1 ? index : Integer.MAX_VALUE;}));

结果

[OwnerClass(description=, id=12), OwnerClass(description=, id=34), OwnerClass(description=, id=54), OwnerClass(description=, id=22), OwnerClass(description=, id=33),
OwnerClass(description=,  id=100)]

 类似资料: