title: Can I do a binary search on an array of objects?
body: <p>I am currently learning how to use searching and sorting algorithms and I am running into issues with a binary search on an array of objects of customers' data. The array of customers is sorted by first and last name.</p>
  <p>The goal is to find a customer's email and return the index.</p>
  <p>The data looks like:</p>
  <pre><code>[
    {
      &quot;username&quot;: &quot;Maude.Torp&quot;,
      &quot;email&quot;: &quot;Taya.Kerluke53@gmail.com&quot;,
      &quot;address&quot;: {
        &quot;street&quot;: &quot;Rowe Fields&quot;,
        &quot;suite&quot;: &quot;Suite 231&quot;,
        &quot;city&quot;: &quot;Tiannamouth&quot;,
        &quot;zipcode&quot;: &quot;07584-6653&quot;,
        &quot;geo&quot;: { &quot;lat&quot;: &quot;75.0283&quot;, &quot;lng&quot;: &quot;-17.1824&quot; }
      },
      &quot;phone&quot;: &quot;795-827-5446 x18366&quot;,
      &quot;website&quot;: &quot;nico.com&quot;,
      &quot;company&quot;: {
        &quot;name&quot;: &quot;Champlin, Feest and Barrows&quot;,
        &quot;catchPhrase&quot;: &quot;Object-based user-facing orchestration&quot;,
        &quot;bs&quot;: &quot;transition integrated content&quot;
      },
      &quot;firstName&quot;: &quot;Maida&quot;,
      &quot;lastName&quot;: &quot;Feeney&quot;
    },
    ...
  ]
  </code></pre>
  <p>My binary search function looks like:</p>
  <pre><code>function searchByEmail(email, customers) {
    let start = 0;
    let end = customers.length - 1;
  
    while (start &lt;= end) {
      let middle = Math.floor((start + end) / 2);
  
      if (customers[middle] === email) {
        return middle;
      } else if (customers[middle] &lt; email) {
        start = middle + 1;
      } else {
        end = middle - 1;
      }
    }
    return -1;
  }
  </code></pre>
  <p>On a sorted array of numbers I am able to return the index of the desired key.</p>
  <pre><code>let customers = [1, 10, 45, 56, 66, 567]
  ...
  console.log(searchByEmail(66, customers))
  
  // --&gt; 4
  </code></pre>
  <p>When I look through this array of objects I only return -1. How do I change this algorithm to work for objects within arrays?</p>