I am working on a programming challenge and I have already looked at this topics before asking:
Sorting elements of vector where each element is a pair [duplicate]
How do I sort a vector of pairs based on the second element of the pair?
And the situation is like this:
-I have my vector of pairs: vector< pair<int, int> > rank;
-And I have already implemented a predicate to compare and sort the vector of pairs by the second element and in descending order:
struct predicate
{
bool operator()(const std::pair<int, int> &left, const std::pair<int, int> &right)
{
return left.second < right.second;
}
}
sort(rank.rbegin(), rank.rend(), predicate());
The programming challenge will give repeated values for the second element, and for that cases, I must leave the first element ordered by the time it was inserted to the vector of pairs, for example:
K V 1 3 2 4 4 5 33 3
Sorted must be:
4 5 2 4 1 3 33 3
The problem comes when I test my solution with a test case I designed:
K V 1 2 16 3 11 2 20 3 18 2 39 39 23 22 12 19 123 4 145 6 3 5 26 4 9574 4 7 1 135 5 193 99 18237 3 22 4 1293 3 3471 33
It's supposed the output should be like this, after sorting the vector:
193 99 39 39 3471 33 23 22 12 19 145 6 3 5 135 5 123 4 26 4 9574 4 22 4 16 3 20 3 18237 3 1293 3 1 2 11 2 18 2 7 1
But instead of that, I got some elements ordered by the first value too:
193 99 39 39 3471 33 23 22 12 19 145 6 3 5 135 5 123 4 26 4 9574 4 22 4 20 3 ->Changed element 16 3 ->Changed element 18237 3 1293 3 18 2 ->Changed element 11 2 1 2 ->Changed element 7 1
Why is happening this?? What am I doing wrong?? Help will be appreciated