Here is a sample of my JSON data:
[
{
"cash": 100,
"uid": "LHy2qRGaf3nkWQgU4axO",
"name": "test2"
},
{
"cash": 1000000,
"uid": "01wFhCSlnd9vSDY4NIkx",
"name": "test"
},
{
"cash": 500,
"uid": "PBOhla0jPwI4PIeNmmPg",
"name": "test3"
}
]
I am attempting to sort the JSON objects by the user's cash amount. Here is the code snippet I used:
var objArr = []; // array containing JSON objects
function compare(a, b) {
console.log("a" + a.cash);
console.log("b" + b.cash);
if (a.cash > b.cash)
return -1;
if (a.cash < b.cash)
return 1;
return 0;
}
var obj = objArr.sort(compare);
response.send(obj);
However, the response returned is not ordered as expected. Can anyone help me understand how to correct this issue? Thank you.