title: How to sort an array by frequency of occurrence?
body: <p>I've array like this :</p>
  
  <pre><code>people = [
  {id:1,name:'abc',age:18},
  {id:2,name:'abcd',age:20},
  {id:3,name:'abce',age:18},
  {id:4,name:'abcf',age:18},
  {id:5,name:'abcg',age:21},
  {id:6,name:'abch',age:20},
  {id:7,name:'abci',age:17},
  ]
  </code></pre>
  
  <p>Now I want Sorting an array order by frequency of occurence(age) in JavaScript.
  Output like this: </p>
  
  <pre><code>people = [
  {id:1,name:'abc',age:18},
  {id:3,name:'abce',age:18},
  {id:4,name:'abcf',age:18},
  {id:2,name:'abcd',age:20},
  {id:6,name:'abch',age:20},
  {id:5,name:'abcg',age:21},
  {id:7,name:'abci',age:17},
  ]
  </code></pre>
  
  <p>So how I can do it ?</p>
  
  <p>I've tried follow answer of Nina Scholz but i've a bug like this. </p>
  
  <pre><code>var people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abcg', age: 21 }, { id: 8, name: 'abci', age: 17 }],
  count = people.reduce((r, { age }) =&gt; (r[age] = (r[age] || 0) + 1, r), {});
  
  people.sort((a, b) =&gt; count[b.age] - count[a.age]);
  
  console.log(people);
  </code></pre>
  
  <p>Output is :</p>
  
  <pre><code>people = [
  {id:1,name:'abc',age:18},
  {id:3,name:'abce',age:18},
  {id:4,name:'abcf',age:18},
  {id:2,name:'abcd',age:20},
  {id:5,name:'abcg',age:21},
  {id:6,name:'abch',age:20},
  {id:5,name:'abcg',age:21},
  {id:7,name:'abci',age:17},
  ]
  </code></pre>