I have a vector of values. I want to get a sorted list of indices out based on these values.
I have got this working reasonably well except where the same value occurs. When the same value occurs I'd like the indices to stay in order.
For example I have this test case:
std::vector< size_t > idx;
std::vector< int > val;
for( int i = 0; i < 40; i++ )
{
idx.push_back( i );
val.push_back( i % 10 );
}
std::sort( idx.begin(), idx.end(), [&]( size_t a, size_t b )
{
return val[a] < val[b];
} );
This sorts the index array to the following:
(0,10,30,20,1,31,21,11,2,22,12,32,3,13,23,33,4,14,24,34,5,15,25,35,6,16,26,36,7,17,27,37,8,28,18,38,9,29,19,39)
But I want the array to be in the following order:
(0,10,20,30,1,11,21,31,2,12,22,32,3,13,23,33,4,14,24,34,5,15,25,35,6,16,26,36,7,17,27,37,8,18,28,38,9,19,29,39)
Is there an easy way I can modify my lambda to get these values in the order specified by the last?
Cheers in advance!