title: Why STL unordered_map and unordered_set cannot be sorted by STL algorithms?
body: <p>I'll start by illustrating a simple use case example:</p>
  
  <ul>
  <li><p>Consider the problem of a social security ID database, where in C++ code is modelled as a <code>std::unordered_map</code> where its key is the social security ID of a person and its value is a <code>std::string</code> with the full-name of that person (e.g., <code>std::unordered_map&lt;int, std::string&gt; DB;</code>).</p></li>
  <li><p>Consider also, that there's a request for printing this database sorted in ascending order based on the person's ID (i.e., <code>std::unordered_map</code>'s key).</p></li>
  <li><p>Naively, one would think to use <code>std::sort</code> in order to sort the <code>std::unordered_map</code> according to the requested criteria and then print it, like the example code below:</p></li>
  </ul>
  
  <hr>
  
  <pre><code>   std::sort(DB.begin(), DB.end());
     for(auto p : DB) std::cout &lt;&lt; "ID(" &lt;&lt; p.first
                                &lt;&lt; ") - " 
                                &lt;&lt; p.second 
                                &lt;&lt; std::endl;
  </code></pre>
  
  <hr>
  
  <ul>
  <li>However, this is not the case, because use of <code>std::sort</code> with a range of either a <code>std::unordered_map</code> or a <code>std::unordered_set</code> will raise a compiler error.</li>
  </ul>
  
  <h2>Questions:</h2>
  
  <ol>
  <li>Why STL's unordered containers cannot be sorted by <code>std::sort</code>?</li>
  <li>Is there a legitimate and efficient way to sort either a <code>std::unordered_map</code> or a <code>std::unordered_set</code>?</li>
  </ol>