title: JavaScript sort an array of objects based array of properties
body: <p>I have for example this dataset:</p>
  <pre><code>const order = [
    { key: &quot;job&quot;, direction: &quot;ascending&quot; },
    { key: &quot;age&quot;, direction: &quot;descending&quot; },
  ];
  
  const records = [
    { name: &quot;christian&quot;, age: 40, job: &quot;developer&quot; },
    { name: &quot;andrew&quot;, age: 48, job: &quot;developer&quot; },
    { name: &quot;elisabeth&quot;, age: 31, job: &quot;floor manager&quot; },
    { name: &quot;oscar&quot;, age: 61, job: &quot;floor manager&quot; },
    { name: &quot;gisela&quot;, age: 51, job: &quot;area manager&quot; },
    { name: &quot;buffy&quot;, age: 27, job: &quot;trainee&quot; },
    { name: &quot;carl&quot;, age: 23, job: &quot;trainee&quot; },
  ];
  </code></pre>
  <p>I need to sort the <code>records</code> array according to the criteria from <code>order</code> array.
  I ended up with this solution:</p>
  <pre><code>const sorted = records.sort(
    (recordA, recordB) =&gt;
      recordA.job.localeCompare(recordB.job) || recordA.age - recordB.age
  );
  </code></pre>
  <p>But I cant understand how can I use the <code>order</code> array instead of hardcoded the <code>job</code> and <code>age</code> properties. The <code>order</code> array can have many properties.</p>