Can we sort the unordered_map on the basis of VALUE in c++ using inbuilt sort function ?
Sorting of unordered_map
Hi @poorva2145
This is impossible from both a compilation and logical standpoint. From a type standpoint, std::sort requires:
-RandomIt must meet the requirements of ValueSwappable and RandomAccessIterator.
-The type of dereferenced RandomIt must meet the requirements of MoveAssignable and MoveConstructible.
From a logical standpoint, sorting an unordered container makes no sense.
If you want to “sort” your unordered_map, put them in a vector:
vector<pair<char, int> > elems(table.begin(), table.end());
sort(elems.begin(), elems.end());
1 Like