title: how to sort an array of objects with different keys
body: <p>I'm trying to sort an array of objects by a key in a particular position. The only problem is that each object has a different key name. </p>
  
  <p>here is an example of an object i'm trying to sort:</p>
  
  <pre><code>let data = [
    {name: "James", x: 3, }, 
    {name: "Thomas", y: 1}, 
    {name: "Zack", z: 2}
  ];
  </code></pre>
  
  <p>I'm trying to sort it by the 2nd key so the order should be </p>
  
  <pre><code>    [
        {name: "James", x: 3, }, 
        {name: "Zack", z: 2},
        {name: "Thomas", y: 1} 
      ];
  </code></pre>
  
  <p>here is how I'm trying to do it:</p>
  
  <pre><code>let data = [
        {name: "James", x: 3, }, 
        {name: "Thomas", y: 1}, 
        {name: "Zack", z: 2}
      ];
  
  data.sort((a, b) =&gt; {
    let key1 = Object.keys(a)[1];
    let key2 = Object.keys(b)[1];
    return a[key1] &gt; b[key2]
    });
  console.log(data)
  </code></pre>
  
  <p>Here is my jsbin</p>
  
  <p><a href="https://jsbin.com/lihefodoni/edit?html,js,console" rel="nofollow noreferrer">https://jsbin.com/lihefodoni/edit?html,js,console</a></p>
  
  <p>Not sure why it's not working. I'm trying to do this in my react Application so I don't know if there's something different I need to do?</p>