how do I reorder the list say:
['apple', 'banana', 'orange']
if user select banana, the list becomes
['banana', 'apple', 'orange']
Another way to do it:
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)
Split into two lists and recombine - easily generalized for non-String lists:
List<String> moveToStart(List<String> original, String input) {
original.split {it.equals(input)}.flatten()
}
List pickToFirst(List list, int n) {
return list[n,0..n-1,n+1..list.size()-1]
}
In your case,
def list = ['apple', 'banana', 'orange']
def newList = pickToFirst(list, 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With