In a previous question, an answer showed how to use lambdas to track original positions in an array and just sort the indices.
C++ sorting and keeping track of indexes
I want to sort the list but store the original indices. One approach is to sort the indices, then reorder the list. This would require two passes, and the second is O(n) so perhaps that is not too bad. But I wondered whether it would be possible to simultaneously reorder the values and swap the list. The problem is that the lambda for comparison only takes two parameters.
What is the best way to write:
template<typename T>
sortWithIndexes(v, indexes, n) {
}
so that v is reordered, and indexes contains the original position of each element of v?