W zasadzie to co @tangar napisał wyczerpuje temat, ale możesz użyć std::pair:
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
int main() {
using index_value = std::pair<unsigned, int>;
std::vector<index_value> vec;
vec.emplace_back(0, 3);
vec.emplace_back(1, 1);
vec.emplace_back(2, 4);
vec.emplace_back(3, 2);
vec.emplace_back(4, 9);
std::sort(vec.begin(), vec.end(),
[](auto const& rhs, auto const& lhs) { return rhs.second < lhs.second; });
//std::ranges::sort(vec, {}, &index_value::second); C++20
for (auto [index, value] : vec) {
std::cout << index << " " << value << "\n";
}
}
https://stackoverflow.com/questions/279854/how-do-i-sort-a-vector-of-pairs-based-on-the-second-element-of-the-pair