While there are simpler methods available for this specific example, in a more general scenario you have the option to utilize a function with the sort method to compare each pair of values in a defined order. (View example on jsfiddle)
var arr1 = [[6, 0.75], [6, 0.81], [9, 0.75], [4, 0.20]],
arr2 = [[6, 0.75], [6, 0.81], [9, 0.75], [4, 0.20]],
people = [{name: 'Jim', age: 40}, {name: 'Sally', age: 35},
{name: 'Tim', age: 20}, {name: 'Jim', age: 72}];
function orderedComparison(indices) {
return function(a, b) {
for (var i = 0; i < indices.length; i++) {
if (a[indices[i]] > b[indices[i]]) return 1;
if (a[indices[i]] < b[indices[i]]) return -1;
// (if a == b, check next index)
}
}
}
// sorting by first item in each pair followed by second
arr1.sort(orderedComparison([0, 1]));
console.log(arr1);
// sorting by second item then first
arr2.sort(orderedComparison([1, 0]));
console.log(arr2);
people.sort(orderedComparison(['name', 'age']));
console.log(people);
It is important to note that simply sorting by the lower-priority key first and then the higher-priority key may work in some cases but is not always guaranteed.
arr1.sort(function(a, b) {return a[1] - b[1]});
arr1.sort(function(a, b) {return a[0] - b[0]});