If you have a list of things, for example:
def list = [ 'c', 'a', 'b' ]
One way to find out the source index is to use transpose() to combine this list into a counter, then sort the new list, then you will have a sorted list with the source index as a secondary element
t
[list,0..<list.size()].transpose().sort { it[0] }.each { item, index -> println "$item (was at position $index)" }
To break it down
[list,0..<list.size()]
gives (effectively) a new list [ [ 'c', 'a', 'b' ], [ 0, 1, 2 ] ]
calling transpose() on this gives us: [ [ 'c', 0 ], [ 'a', 1 ], [ 'b', 2 ] ]
Then we sort the list based on the first element in each element (letters from our original list) with sort { it[0] }
And then iterate over each of them, printing out our sorted element and its original index location
source share