Javascript sort numbers with a variable for the field being sorted
<p>I am trying to sort some numbers in Javascript - this works fine, but I am trying to refactor some of the code so I am not hard coding field names.  Below is my function:</p>

<pre><code>data.sort(function(a, b) {
    return a.values[0].field1 - b.values[0].field1;
});
</code></pre>

<p>The problem is that I want to also use this same functionality with <code>field2</code></p>

<p>I've tried doing something like the following (simplified):</p>

<pre><code>var sortBy = "field1";

data.sort(function(a, b) {
    return a.values[0].sortBy- b.values[0].sortBy;
});
</code></pre>

<p>That is looking for a literal field name of sortBy though and is not working.  Any help would be appreciated.</p>