The sort in c++ can only sort the vector, so if you customize the unordered_map sort, you need to put it in the vector.
Take lc386 as an example:
class Solution {
public:
static bool cmp(pair<string, int> &a, pair<string, int> &b){
return a.first < b.first;
}
vector<int> lexicalOrder(int n) {
vector<int> result;
vector<pair<string, int>> tVector;
unordered_map<string, int> mapp;
for(int i = 0;i<n;i++){
mapp[to_string(i+1)] = i+1;
}
for(auto iter = mapp.begin();iter!=mapp.end();iter++){
tVector.push_back(make_pair(iter->first, iter->second));
}
sort(tVector.begin(), tVector.end(), cmp);
for(auto it = tVector.begin();it!=tVector.end();it++){
result.push_back(it->second);
}
return result;
}
};