比方说,我有一份清单:
unordered_list = ['c-1','a-2','a-4','b-2','a-1','b-3','c-3','c-4']我有一些条件:
结果应该是:
ordered_list = ['b-3','c-3','a-1','a-2','a-4','b-2','c-1','c-4']我花了很多时间在Groovy中找到一些实现它的有效方法,但是没有成功,因为我是它的初学者。任何提示都将不胜感激。提前感谢!
发布于 2017-04-13 13:36:03
不需要所有其他列表,您可以直接流数组。
Stream.concat(Stream.of(unordered_list)
.filter(s -> s.endsWith("3"))
.sorted(),
Stream.of(unordered_list))
.filter(s -> !s.endsWith("3"))
.sorted()
.collect(toList());或实际名单:
List<String> unorderedList = asList("c-1", "a-2", "a-4", "b-2", "a-1", "b-3", "c-3", "c-4");
Stream.concat(unorderedList.stream()
.filter(s -> s.endsWith("3"))
.sorted(),
unorderedList.stream()
.filter(s1 -> !s1.endsWith("3"))
.sorted())
.collect(toList());最后,另一种使用分区的方法
Map<Boolean, List<String>> endsWith3Partition = Stream.of(unordered_list)
.sorted()
.collect(partitioningBy(s -> s.endsWith("3")));
List<String> sorted = new ArrayList<>(unordered.length);
sorted.addAll(endsWith3Partition.get(true));
sorted.addAll(endsWith3Partition.get(false));发布于 2017-04-13 13:26:08
您可以编写一个比较器,如下所示:
Arrays.sort(unordered_list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String[] o1s = o1.split('-');
String[] o2s = o2.split('-');
boolean end1_3 = o1s[1].equals("3");
boolean end2_3 = o2s[1].equals("3");
if(end1_3 && end2_3) {
return 0; //both end with 3
} else if(end1_3) {
return -1; //only the first ends with 3, so less than
} else if(end2_3) {
return 1; //only the second ends with 3, so greater than
}
if(!o1s[0].equals(o2s[0])) { // first group not same
return o1s[0].compareTo(o2s[0]); // compare first groups
}
return o1s[1].compareTo(o2s[1]); // assume equal
}
});发布于 2017-04-13 13:28:19
您可以使用Java 8的stream来完成它,例如:
List<String> list = Arrays.asList(new String[]{"c-1","a-2","a-4","b-2","a-1","b-3","c-3","c-4"});
TreeMap<Boolean, List<String>> lists = list.stream()
.collect(Collectors.groupingBy(s -> s.toString().endsWith("3"), TreeMap::new, Collectors.toList()));
final List<String> result = new ArrayList<>();
lists.descendingMap().forEach((k, v) -> {
Collections.sort(v);
result.addAll(v);
});
System.out.println(result);https://stackoverflow.com/questions/43393384
复制相似问题