I need to use JavaScript to sort this sample data by either storeID number or the number of transactions:
transactionList = [
{storeID: 102, transactions: 6},
{storeID: 64, transactions: 22},
{storeID: 295, transactions: 9},
{storeID: 83, transactions: 4},
{storeID: 96, transactions: 14}
];
Here is the function I was trying to build, where sort modifier is either "storeID" or "transactions":
function sortTransactions (modifier, transactionList) {
return transactionList.sort(function(x, y) {
return x.modifier - y.modifier;
});
}
So if I run this in console:
sortTransactions("storeID", transactionList);
I expected that my transactionList would be sorted. But the function just returns the initial value.
When I injected console.log(x.modifier, y.modifier) into the mix, it shows a value of undefined, so clearly I'm doing one or more things wrong here.
I had searched and found this to be the essence of what I was going for, but couldn't seem to translate that into my own working function: Javascript object list sorting by object property
Any help is very much appreciated!