title: Ordering string occurence by count DESC javascript
body: <p>i created a simple <strong>hashtags tool</strong> with js in local.</p>
  
  <p>Extract from text, count and order hashtags by count DESC in js local.</p>
  
  <p><strong>Problem</strong>: The ordering is based on alphabetic occurence and i want to sort by count DESC (like sql query) see the result on demo link</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;h2&gt;#Hashtag Cloud&lt;/h2&gt; 
  &lt;button onclick="extractHashtags()"&gt;Extract #hashtags&lt;/button&gt;
  &lt;button onclick="countHashtags()"&gt;Count #hashtags&lt;/button&gt;
  &lt;textarea id="inputTextToSave" cols="100" rows="5"&gt;#Hello this #is 
  a text with #lot of #hashtags #hello #hello #The #quick #brown #fox 
  #jumps #over #the #lazy #dog #The quick brown fox jumps over #the 
  #lazy dog&lt;/textarea&gt;
  </code></pre>
  
  <p><strong>JS</strong></p>
  
  <pre><code>function extractHashtags(){
    var textToSave = document.getElementById("inputTextToSave").value;
    textToSave = textToSave.toLowerCase()
    var number_regex = /((?:|^\s)(?:#)([a-zA-Zà-úÀ-Ú\d]+))/g;
    var matches = [];
    textToSave.replace(number_regex, function(match)
      {
      matches.push(match);
      });
    //alert(matches);
    document.getElementById("inputTextToSave").value = matches;  
  }
  
  function wordFreq(string) {
      var words = string.replace(/[.]/g, '').split(/\s/);
      var freqMap = {};
      words.forEach(function(w) {
          if (!freqMap[w]) {
              freqMap[w] = 0;
          }
          freqMap[w] += 1;
      });
  
      return freqMap;
  }
  
  function countHashtags(){
    var $inputTemp = document.getElementById("inputTextToSave").value; 
    $inputTemp = $inputTemp.replace(/,/gi, ' ');
  
    var freq = wordFreq($inputTemp);
    var $test ='';
    Object.keys(freq).sort().forEach(function(word)
      {
      // $test += word + "(" + freq[word] + "), ";
      // occurence: 'count',
      $test += word + ":" + freq[word] + ", ";
      });
  
    document.getElementById("inputTextToSave").value = $test;
  }
  </code></pre>
  
  <p>All the code below come from different stackoverflow answers. </p>
  
  <p><a href="https://codepen.io/keusta/pen/GxRbLK" rel="nofollow noreferrer">Here is a working demo</a> on codepen</p>
  
  <p>Before asking here, i tried <a href="https://stackoverflow.com/a/3824938/9467239">this answer</a> but the result is wrong when numbers over 10 are ordered with the rest (exemple : 1 &lt; 12 &lt; 2 &lt; 28 &lt; 30</p>