title: Sorting by pre defined order with multiple properties
body: <p>I want to sort attendees based on the key <code>groups</code> which is an array of groups (function like tags). I want to sort by the predefined <code>sortOrder</code> order and then by if main_contact is true if multiple attendees for the same group exist.
  But I'm really stuck with how to check if a groups.name exists and then sort them efficiently by the predefined order.</p>
  <pre><code>const attendees = [
    {
      name: &quot;Peter&quot;,
      groups: [{ name: &quot;agency&quot;, main_contact: false, stand_in_contact: true }],
    },
    {
      name: &quot;Alex&quot;,
      groups: [{ name: &quot;agency&quot;, main_contact: true, stand_in_contact: false }],
    },
    {
      name: &quot;Nina&quot;,
      groups: [
        { name: &quot;production&quot;, main_contact: true, stand_in_contact: false },
      ],
    },
    {
      name: &quot;Christina&quot;,
      groups: [
        { name: &quot;client&quot;, main_contact: true, stand_in_contact: false },
        { name: &quot;crew&quot;, main_contact: false, stand_in_contact: false },
      ],
    },
  ];
  
  const sortAttendees = (attendees) =&gt; {
    const sortOrder = [&quot;agency&quot;, &quot;client&quot;, &quot;production&quot;, &quot;crew&quot;];
    // return sortOrder.indexOf(a.type) - sortOrder.indexOf(b.type);
    if (!attendees || attendees.length == 0) return [];
    return attendees.sort((a, b) =&gt; {
      if (a.groups.some((group) =&gt; group.name === &quot;agency&quot;)) {
        return -1;
      }
      if (a.groups.some((group) =&gt; group.name === &quot;client&quot;)) {
        return 1;
      }
      if (a.groups.some((group) =&gt; group.name === &quot;production&quot;)) {
        return 1;
      }
      return 1;
    });
  };
  </code></pre>
  <p><strong>Result</strong></p>
  <pre><code>const attendees = [
    {
      name: &quot;Alex&quot;,
      groups: [{ name: &quot;agency&quot;, main_contact: true, stand_in_contact: false }],
    },
    {
      name: &quot;Peter&quot;,
      groups: [{ name: &quot;agency&quot;, main_contact: false, stand_in_contact: true }],
    },
    {
      name: &quot;Christina&quot;,
      groups: [
        { name: &quot;client&quot;, main_contact: true, stand_in_contact: false },
        { name: &quot;crew&quot;, main_contact: false, stand_in_contact: false },
      ],
    },
    {
      name: &quot;Nina&quot;,
      groups: [
        { name: &quot;production&quot;, main_contact: true, stand_in_contact: false },
      ],
    },
  ];
  </code></pre>