how to sort an array and keep track of the index