これはあなたが望むものとは正確に一致しないかもしれませんが、それはあなたに先に進むのに十分なアイデアを与えるはずです:
var list = [{
prop1: 'a',
prop2: 5
}, {
prop1: 'b',
prop2: 4
}, {
prop1: 'c',
prop2: 3
}, {
prop1: 'd',
prop2: 2
}, {
prop1: 'e',
prop2: 1
}];
function sort(propName, direction) {
var dirOffset = direction === 'desc' ? -1 : 1;
list.sort(function(a, b){
a = a[propName];
b = b[propName];
if (a === b) {
return 0;
}
var out = a < b ? -1 : 1;
return out * dirOffset;
});
}
function log(propName) {
list.forEach(function(item){
console.log(item[propName]);
});
}
function sortAndLog(propName, direction) {
console.log('Sorted by', propName, direction);
sort(propName, direction);
log(propName);
console.log('------------------------------------');
}
sortAndLog('prop1', 'desc');
sortAndLog('prop1', 'asc');
sortAndLog('prop2', 'asc');
sortAndLog('prop2', 'desc');