如何重新排序列表说:
['apple', 'banana', 'orange']
如果用户选择banana,则列表变为
['banana', 'apple', 'orange']
答案 0 :(得分:5)
另一种方法:
def list = ['apple', 'banana', 'orange']
// get a reference to the selection (banana)
def pick = list.find { it == 'banana' }
// remove selection from the the list
def newList = list.minus(pick)
// add selection back at the beginning
newList.add(0, pick)
答案 1 :(得分:2)
List pickToFirst(List list, int n) {
return list[n,0..n-1,n+1..list.size()-1]
}
在你的情况下,
def list = ['apple', 'banana', 'orange']
def newList = pickToFirst(list, 1)
答案 2 :(得分:1)
分成两个列表并重新组合 - 很容易推广非字符串列表:
List<String> moveToStart(List<String> original, String input) {
list.split {it.equals(input)}.flatten()
}