title: Return sorted unique values from array, but if count is equal, return values in order
body: <p>I am creating a function that takes in an array of unsorted integers and returns an array of the unique integers, sorted by frequency. However, if the integers have the same frequency, they will be returned in the original order of the input array. Here is my current function:</p>
  
  <pre><code>function uniqueUnionSorted(arr) {
    counter = {};
    for(var i=0; i&lt;arr.length; i++) {
      if (arr[i] in counter) {
        counter[arr[i]] ++;
      } else {
        counter[arr[i]] = 1;
      }
    }
    sortedStrings = Object.keys(counter).sort(function(a,b) {
      return counter[b] - counter[a]
    });
    var sortedNumbers = sortedStrings.map(Number);
    return sortedNumbers;
  }
  </code></pre>
  
  <p>So for an array like this:</p>
  
  <pre><code>arr = [1, 3, 2, 1, 5, 2, 1, 4]
  </code></pre>
  
  <p>the function should return:</p>
  
  <pre><code>[1,2,3,5,4]
  </code></pre>
  
  <p>However, my function is sorting the 5 and 4 and is returning:</p>
  
  <pre><code>[1,2,3,4,5]
  </code></pre>
  
  <p>Please help!</p>