title: sort array of objects by number of object fields matched
body: <p>I have the following object:</p>
  
  <pre><code>var entries = [
    {
      fields: {
        tags: [
          { fields: { id: "football" } },
          { fields: { id: "running" } },
          { fields: { id: "tennis" } }
        ]
      }
    },
    {
      fields: {
        tags: [
          { fields: { id: "tennis" } },
          { fields: { id: "football" } },
          { fields: { id: "rugby" } }
        ]
      }
    },
    {
      fields: {
        tags: [
          { fields: { id: "cricket" } },
          { fields: { id: "running" } }
        ]
      }
    },
    {
      fields: {
        tags: [
          { fields: { id: "football" } },
          { fields: { id: "rugby" } }
        ]
      }
    }
  ];
  </code></pre>
  
  <p>Now, I want to sort the array using another array: <code>var testArray = ["football", "rugby"]</code></p>
  
  <p>So any of the objects that contain both <code>rugby</code> and <code>football</code> in their <code>fields.tags.fields.id</code> should return first in the array, then any that just contain 1 should appear next. So the results I'm after would be:</p>
  
  <pre><code>var sortedEntries = [
    {
      fields: {
        tags: [
          { fields: { id: "tennis" } },
          { fields: { id: "football" } },
          { fields: { id: "rugby" } }
        ]
      }
    },
    {
      fields: {
        tags: [
          { fields: { id: "football" } },
          { fields: { id: "rugby" } }
        ]
      }
    },
    {
      fields: {
        tags: [
          { fields: { id: "football" } },
          { fields: { id: "running" } },
          { fields: { id: "tennis" } }
        ]
      }
    },
    {
      fields: {
        tags: [
          { fields: { id: "cricket" } },
          { fields: { id: "running" } }
        ]
      }
    }
  ];
  </code></pre>
  
  <p>I thought this might be possible using the <code>sort()</code> function but then I realised I need to compare my <code>testArray</code> to see how many items are matched. Is this possible? I can't find any examples online of what I'm trying to do and I don't know where to begin.</p>