title: Combine two arrays on primary key and foreign key and sort data
body: <p>I have two arrays like this:</p>
  <pre><code>let employees = [
      {
          &quot;id&quot;:&quot;1&quot;,
          &quot;name&quot; : &quot;John Doe&quot;
      },
      {
          &quot;id&quot;:&quot;4&quot;,
          &quot;name&quot; : &quot;Peter Jones&quot;
      },
      {
          &quot;id&quot;:&quot;3&quot;,
          &quot;name&quot; : &quot;Jack Johnson&quot;
      },
      {
          &quot;id&quot;:&quot;2&quot;,
          &quot;name&quot; : &quot;Ron Morris&quot;
      }
  ]
  
  let salaries = [
      {
          &quot;employeeId&quot; : &quot;1&quot;,
          &quot;salary&quot;: &quot;1500&quot;
      },
      {
          &quot;employeeId&quot; : &quot;2&quot;,
          &quot;salary&quot;: &quot;150&quot;
      },
      {
          &quot;employeeId&quot; : &quot;3&quot;,
          &quot;salary&quot;: null 
      },
      {
          &quot;employeeId&quot; : &quot;4&quot;,
          &quot;salary&quot;: &quot;1780&quot;
      }
  ]
  </code></pre>
  <p>Result should be an exposed function for retrieving employees with their salaries from the server.</p>
  <p>Function should support returning employees ascending or descending by salary.</p>
  <p>I tried something like this but it does not work:</p>
  <pre><code>let result = employees.map(e =&gt; ({...e, salary: salaries.filter(({ employeeId }) =&gt; employeeId === e.id).sort((a,b) =&gt; (a.salary &gt; b.salary) ? 1 : ((b.salary &gt; a.salary) ? -1 : 0))}));
  </code></pre>
  <p>Thanks in advance.</p>