title: Define custom parameters inside javascript compare functions in JavaScript?
body: <p>I have an array of objects that needs sorting by last name, however the object only has firstname and fullname parameters, so for last name I have to use <code>fullname.replace((firstname + &quot; &quot;), &quot;&quot;)</code>. Example array below.</p>
  <pre><code>const names = [
          {
              firstname: &quot;John&quot;,
              fullname: &quot;John Doe&quot;
          },
          {
              firstname: &quot;Amber&quot;,
              fullname: &quot;Amber Smith&quot;
          },
          {
              firstname: &quot;Michael&quot;,
              fullname: &quot;Michael Smith&quot;
          },
          {
              firstname: &quot;Jessica&quot;,
              fullname: &quot;Jessica Kelly Charles&quot;
          }
      ]
  </code></pre>
  <p>While I can use the &quot;replace&quot; every time inside a sort compare function, I'd much rather have something like this:</p>
  <pre><code>names.sort(function(a, b) {
              const lastname = fullname.replace((firstname+ &quot; &quot;), &quot;&quot;);
              if (a.lastname &lt; b.lastname) {
                  return -1;
              } else if (a.lastname &gt; b.lastname) {
                  return 1;
              }
              return 0;
          });
  </code></pre>
  <p>Obviously lastname comes up as undefined.
  This has been rather hard to google for, and I think I'm missing some JavaScript basics here, but would greatly apprecite your help in helping me learn to write better code.</p>