I have done this several times in different situations. Instead of sorting the array, just create a new array that has sorted indexes in it.
For example, you have the length n of the vector (vector), as well as the 2d nxn array. Create a new array index containing the values [0, n-1].
Then, instead of accessing evals as evals [i], you get access to it as evals [index [i]], and instead of evects [i] [j] you get access to it [index [i]] [j].
Now you are writing your sort procedure to sort the index, not the evals array, so instead of an index similar to {0, 1, 2, ..., n-1}, the value in the index array will be in ascending order of values in the evals array.
So, after sorting, if you do this:
for (int i=0;i<n;++i) { cout << evals[index[i]] << endl; }
You will get a sorted list of ratings.
this way you can sort everything related to this parsing array without actually moving the memory. This is important when n gets big, you don’t want to move around the columns of the evects matrix.
basically, the i-th smallest eval will be located at index [i] and corresponds to index [i] th evect.
Edited to add. Here's the sort function I wrote to work with std :: sort to do what I just said:
template <class DataType, class IndexType> class SortIndicesInc { protected: DataType* mData; public: SortIndicesInc(DataType* Data) : mData(Data) {} Bool operator()(const IndexType& i, const IndexType& j) const { return mData[i]<mData[j]; } };