Jenkins 根据复杂条件列出元素顺序
比如说,我有一个清单:Jenkins 根据复杂条件列出元素顺序,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,比如说,我有一个清单: unordered_list = ['c-1','a-2','a-4','b-2','a-1','b-3','c-3','c-4'] 我有一些条件: 应附加以3结尾的第一个元素(但应保持从a到c的顺序) 然后列表应该从a到c排序 但从a到c排序时,尾端必须从1到3排序 结果应该如下所示: ordered_list = ['b-3','c-3','a-1','a-2','a-4','b-2','c-1','c-4'] 我花了很多时间寻找一些有效的方法在Groovy中实现
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中实现它,但没有成功,因为我是it初学者。
任何提示都将不胜感激。提前谢谢 您可以编写一个比较器,如下所示:
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
}
});
Arrays.sort(无序的_列表,新的比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
字符串[]o1s=o1.split('-');
字符串[]o2s=o2.split('-');
布尔值end1_3=o1s[1]。等于(“3”);
布尔end2_3=o2s[1]。等于(“3”);
if(end1_3&&end2_3){
返回0;//以3结尾
}否则如果(完1_3){
return-1;//只有第一个以3结尾,所以小于
}否则如果(完2_3){
返回1;//只有第二个以3结尾,因此大于
}
如果(!o1s[0].=o2s[0]){//第一组不相同
返回o1s[0]。比较到(o2s[0]);//比较第一组
}
返回o1s[1]。比较(o2s[1]);//假设相等
}
});
您可以编写一个比较器,如下所示:
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
}
});
Arrays.sort(无序的_列表,新的比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
字符串[]o1s=o1.split('-');
字符串[]o2s=o2.split('-');
布尔值end1_3=o1s[1]。等于(“3”);
布尔end2_3=o2s[1]。等于(“3”);
if(end1_3&&end2_3){
返回0;//以3结尾
}否则如果(完1_3){
return-1;//只有第一个以3结尾,所以小于
}否则如果(完2_3){
返回1;//只有第二个以3结尾,因此大于
}
如果(!o1s[0].=o2s[0]){//第一组不相同
返回o1s[0]。比较到(o2s[0]);//比较第一组
}
返回o1s[1]。比较(o2s[1]);//假设相等
}
});
您可以使用Java 8的流来实现,例如:
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);
List List=Arrays.asList(新字符串[]{“c-1”、“a-2”、“a-4”、“b-2”、“a-1”、“b-3”、“c-3”、“c-4”});
TreeMap list=list.stream()
.collect(Collectors.groupby(s->s.toString().endsWith(“3”),TreeMap::new,Collectors.toList());
最终列表结果=新建ArrayList();
lists.downingmap().forEach((k,v)->{
收集.分类(五);
结果:addAll(v);
});
系统输出打印项次(结果);
您可以使用Java 8的流来实现,例如:
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);
List List=Arrays.asList(新字符串[]{“c-1”、“a-2”、“a-4”、“b-2”、“a-1”、“b-3”、“c-3”、“c-4”});
TreeMap list=list.stream()
.collect(Collectors.groupby(s->s.toString().endsWith(“3”),TreeMap::new,Collectors.toList());
最终列表结果=新建ArrayList();
lists.downingmap().forEach((k,v)->{
收集.分类(五);
结果:addAll(v);
});
系统输出打印项次(结果);
不需要所有其他列表,您可以直接对数组进行流式处理
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());
List unorderedList=asList(“c-1”、“a-2”、“a-4”、“b-2”、“a-1”、“b-3”、“c-3”、“c-4”);
Stream.concat(无序列表.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));
Map endsWith3Partition=Stream.of(无序列表)
.已排序()
.收集(按(s->s.endsWith(“3”))分区);
列表排序=新的ArrayList(unordered.length);
sorted.addAll(endsWith3Partition.get(true));
sorted.addAll(endsWith3Partition.get(false));
不需要所有其他列表,您可以直接对数组进行流式处理
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());
List unorderedList=asList(“c-1”、“a-2”、“a-4”、“b-2”、“a-1”、“b-3”、“c-3”、“c-4”);
Stream.concat(无序列表.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));
Map endsWith3Partition=Stream.of(无序列表)
.已排序()
.收集(按(s->s.endsWith(“3”))分区);
列表排序=新的ArrayList(unordered.length);
sorted.addAll(endsWith3Partition.get(true));
sorted.addAll(endsWith3Partition.get(false));
字符串的格式是什么?他们总是三个角色吗long@WillemVanOnsem,不,它们实际上看起来像“-”,其中gitreponame包含2或3个以分隔的单词。字符串的格式是什么?他们总是三个角色吗long@WillemVanOnsem,不,它们实际上看起来像“-”,其中gitreponame包含2或3个单词,以分隔。我认为前两个更有效,因为它们不像分区那样创建中间列表。@我强烈建议,如果有人看到这一点并感到困惑,请将流简化,最初的问题被标记为Java,而不是Groovy,它可能更简单,但在Groovy中类似@Olia对于Groovy,您可以只使用一个类似于比较器belowI的排序闭包。我认为前两个更有效,因为它们不像分区那样创建中间列表。@Olia我强烈建议将流简化如果有人看到这一点并感到困惑,最初的问题被标记为Java,而不是Groovy,它可能更简单,但在Groovy中类似@对于Groovy,您可以只使用一个类似于下面的Comparator的排序闭包