title: How to sort javascript's array of objects by multiple property value
body: <p>I have an array of Objects that contains the <code>fYear</code> and <code>FQtr</code> keys. My goal is to sort by <code>FQtr</code> keys and  <code>fYear</code>. Would you please help?</p>
  <h4>Original Data</h4>
  <pre class="lang-js prettyprint-override"><code>const data = [
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W10&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W11&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W12&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W13&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W2&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q4&quot;, &quot;FQtr&quot;: &quot;W1&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W3&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W4&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q4&quot;, &quot;FQtr&quot;: &quot;W2&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W5&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W6&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W7&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W8&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W9&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q4&quot;, &quot;FQtr&quot;: &quot;W4&quot;},
      { &quot;fYear&quot;: &quot;QTD&quot;, &quot;FQtr&quot;: &quot;QTD&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q4&quot;, &quot;FQtr&quot;: &quot;W3&quot;}
  ]
  </code></pre>
  <h3>Expected Sorted Data</h3>
  <pre class="lang-js prettyprint-override"><code>
  const data = [
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W2&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W3&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W4&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W5&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W6&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W7&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W8&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W9&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W10&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W11&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W12&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q3&quot;, &quot;FQtr&quot;: &quot;W13&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q4&quot;, &quot;FQtr&quot;: &quot;W1&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q4&quot;, &quot;FQtr&quot;: &quot;W2&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q4&quot;, &quot;FQtr&quot;: &quot;W3&quot;},
      { &quot;fYear&quot;: &quot;FY22 Q4&quot;, &quot;FQtr&quot;: &quot;W4&quot;},
      { &quot;fYear&quot;: &quot;QTD&quot;, &quot;FQtr&quot;: &quot;QTD&quot;}
  ]
  </code></pre>
  <h3>My Approach</h3>
  <pre class="lang-js prettyprint-override"><code>function sortFQtr( a, b ) {
      const [prevVal, nextVal] = [Number.parseInt(a.FQtr.substring(1)), Number.parseInt(b.FQtr.substring(1))];
    if ( prevVal &lt; nextVal ) return -1;
    if ( prevVal &gt; nextVal) return 1;
    return 0;
  }
  
  // [&quot;FY22 Q4&quot;, &quot;FY22 Q3&quot;, &quot;QTD&quot;]
   var fYears = data.map(value =&gt; value.fYear).filter( (value, index, _arr) =&gt; _arr.indexOf(value) == index);
  
  </code></pre>