If I understand you correctly, you want to get a soted list of indices. One solution is to create an unordered list of indices of all elements and sort it.
int array[] = {2, 4, 3, 1};
int indices[] = {0, 1, 2, 3};
std::sort(indices, indices + 4, [&](int a, int b) {
return array[b] < array[a];
});
Since you asked for a non-C++11-way, you can work around the lambda expression as shown below:
int array[] = {2, 4, 3, 1};
int indices[] = {0, 1, 2, 3};
struct {
int *array;
bool operator()(int a, int b) const
{
return this->array[b] < this->array[a];
}
} customLess = {array};
std::sort(indices, indices + 4, customLess);
Both implementations will sort the values of indices
but not array
itself. The result would look as shown below:
array == {2, 4, 3, 1}
indices == {1, 2, 0, 3}