title: Sort HashMap according to order of insertion
body: <p>Assuming I have the following array </p>
  
  <blockquote>
    <p>[5,5,2,2,1]</p>
  </blockquote>
  
  <p>I wish to count the number of times each number appear hence I store the count in a hashmap</p>
  
  <pre><code>  arr = [5,5,2,2,1]
    var counter = {}
    for ( var i in arr)
    {
        var value = arr[i]
        counter[value] = (counter[value] || 0) + 1
  
    }
  </code></pre>
  
  <p>The hashmap  looks like this</p>
  
  <blockquote>
    <p>{ '1': 1, '2': 2, '5': 2 }</p>
  </blockquote>
  
  <p>The hashmap is sorted according to the key.</p>
  
  <p>I am wondering how do you sort hashmap according to key and <strong>then sort by order of insertion for keys which has the same value(value as in key value pair)</strong></p>
  
  <p>For the above example, I want to get the following array</p>
  
  <blockquote>
    <p>{ '1': 1, '5': 2, '2': 2 }</p>
  </blockquote>
  
  <p>Though '5' and '2' have the same value(value as in key value pair), '5' is inserted before '2' hence it should come first</p>