Sorting with indices using c++?

indexes should not changed for the numbers but they should be in sorted order

You need to track (keep) the position/index after sorting, Isn’t it? If so, then you need to do something like this:

//You have an array with n elements, say a[n]
pair<int, int> b[n];
for(auto i=0, i<n, i++) b[i] = make_pair(a[i], i);
sort(b, b+n);
//now you can get previous index/position by using b[i].second
cout <<"Minimum value:" << b[0].first << "at position: " << b[0].second;
3 Likes

using a vector of pair is also a good option
int a[n] = { … };
vector<pair<int, int> > vp(n);
for(int i = 0; i < n; i++)
vp[ i ] = make_pair(a[ i ] , i);
sort( vp.begin( ) , vp.end( ) );
// the job will be done