title: Sort array by committee and then by user role
body: <p>I have an array that contains data similar to this:</p>
  
  <pre><code>var committees = [
    {
      committee: "Apple Com",
      userRole: "Member",
      membersNum: 11
    },
      {
      committee: "Orange Com",
      userRole: "Member",
      membersNum: 9
    },    {
      committee: "Banana Com",
      userRole: "",
      membersNum: 13
    },    {
      committee: "Strawberry Com",
      userRole: "Vice President",
      membersNum: 15
    },    {
      committee: "Lemon Com",
      userRole: "",
      membersNum: 13
    },
    {
      committee: "Mango Com",
      userRole: "Chair",
      membersNum: 11
    },
  ]
  </code></pre>
  
  <p>I want them sorted first, by the <strong>User Role</strong> and then by the <strong>Committee</strong>. The <strong>Committee</strong> field will always have a value, while the <strong>User Role</strong> field sometimes will be <strong><em>null</em></strong>. </p>
  
  <p>Currently, what I am doing to achieve this is break the array into two arrays. The first array will return only items which <strong>User Role</strong> is not <strong><em>null</em></strong>. The second array returns items which <strong>User Role</strong> is <strong><em>null</em></strong>. Next, I sort each array by <strong>Committee</strong> ascendingly and then I concatenate them. Thus the logic goes like this:</p>
  
  <pre><code>  //get committees user is member of and committees user is not member of
    var memberOf = [];
    var nonMemberOf = [];
    for(var i=0; i&lt;committees.length; i++){
      if(committees[i].userRole){
        memberOf.push(committees[i]);
      } else {
        nonMemberOf.push(committees[i]);
      }
    }  
    memberOf.sort(function(a,b){return (a.committee &gt; b.committee) ? 1 : -1; });
    nonMemberOf.sort(function(a,b){return (a.committee &gt; b.committee) ? 1 : -1; });
  
    var allComs = memberOf.concat(nonMemberOf);
  </code></pre>
  
  <p>I've spent more than 3 hours reading  and trying to implement what I read on some posts here:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/9175268/javascript-sort-function-sort-by-first-then-by-second">Javascript sort function. Sort by First then by Second</a></li>
  <li><a href="https://stackoverflow.com/questions/10153846/sorting-an-array-of-objects-by-two-properties">Sorting an Array of Objects by two Properties</a></li>
  <li><a href="https://stackoverflow.com/questions/45741397/javascript-sort-array-of-objects-by-2-properties">Javascript - Sort Array of objects by 2 Properties</a></li>
  </ul>
  
  <p>I believe that there is a better way doing what I'm doing but just can't figure it out. If someone can shed some light here, I will appreciate that very much. </p>
  
  <p>P.S. I'm not asking for someone to do this for me, I want to understand how to do it. For those who can help, thanks in advance!</p>