Referring to this question: How to subtract dates from each other
In Groovy, I’ve got a script that spits out the 5 largest values from a text file collection. How can I know what the indices of those values are?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you have a list of things ie:
One way of knowing the original index would be to use
transpose()to join this list to a counter, then sort the new list, then you will have a sorted list with the original index as the secondary elementie:
To break that down;
gives us (effectively) a new list
[ [ 'c', 'a', 'b' ], [ 0, 1, 2 ] ]calling
transpose()on this gives us:[ [ 'c', 0 ], [ 'a', 1 ], [ 'b', 2 ] ]We then sort the list based on the first item in each element (the letters from our original list) with
sort { it[0] }And then iterate through each, printing out our now sorted item, and its original index location