title: How do I sort an array of objects using different arrays of objects?
body: <p>I'm having issues trying to get an array of objects to display in different order. We are supposed to have the output displayed names sorted out by index, first name, last name, and age. When I run the code I only get the names to display once. I've attached a picture of the desire output, I'm a little confused and blocked. Here is what I have so far: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class Names {
  
    constructor(first, last, age) {
      this.first = first;
      this.last = last;
      this.age = age;
    }
  }
  
  function printObject(allNames, sortType, message) {
    if (sortType == "first") {
      allNames.sort(function(a, b) {
        return a.first.localeCompare(b.first);
      });
    }
  
    if (sortType == "last") {
      allNames.sort(function(a, b) {
        return a.last.localeCompare(b.last);
      });
    }
  
    if (sortType == "age") {
      allNames.sort(function(a, b) {
        return a.age - b.age;
      });
    }
  
    for (let i = 0; i &lt; allNames.length; i++) {
      console.log(message);
      console.log(allNames[i].first + " " + allNames[i].last + " " + allNames[i].age);
    }
  }
  
  // main fucntion 
  function main() {
      var allNames = [];
  
      allNames.push(new Names("John", "Doe", 40));
      allNames.push(new Names("Jay", "Thrift", 30));
      allNames.push(new Names("Card", "Ace", 20));
  
      printObject(allNames, "age", "Message");
  }
  
  main();</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="https://i.stack.imgur.com/D9O5G.png" rel="nofollow noreferrer">desire output</a></p>