title: sort by number of occurrence(count) in Javascript array
body: <p>I am new to Jquery and Javascript. Can someone please help me with Jquery sorting based on number of occurrence(count) in array. I tried various sorting methods but none of them worked. </p>
  
  <p>I have an array in Javascript which is</p>
  
  <pre><code>allTypesArray = ["4", "4","2", "2", "2", "6", "2", "6", "6"]
  
  // here  2 is printed four times, 6 is printed thrice, and 4 is printed twice
  </code></pre>
  
  <p>I need output like this</p>
  
  <pre><code>newTypesArray = ["2","6","4"]
  </code></pre>
  
  <p>I tried</p>
  
  <pre><code>function array_count_values(e) {
  var t = {}, n = "",
      r = "";
  var i = function (e) {
      var t = typeof e;
      t = t.toLowerCase();
      if (t === "object") {
          t = "array"
      }
      return t
  };
  var s = function (e) {
      switch (typeof e) {
      case "number":
          if (Math.floor(e) !== e) {
              return
          };
      case "string":
          if (e in this &amp;&amp; this.hasOwnProperty(e)) {
              ++this[e]
          } else {
              this[e] = 1
          }
      }
  };
  r = i(e);
  if (r === "array") {
      for (n in e) {
          if (e.hasOwnProperty(n)) {
              s.call(t, e[n])
          }
      }
  }
  return t
  }
  6: 3
  }
  </code></pre>
  
  <p>output is 
  <code>{4: 2, 2: 6, 6:3}</code></p>