Need help sorting this Array by ID. This code snippet is not working as expected:
var arr = new Array();
arr.push({
id: "9",
datum: "XXX",
start: "XXX",
ziel: "XXX",
hinfahrt: "XXX",
ruckfahrt: "XXX",
zuzahlung: "XXX"
});
arr.push({
id: "3",
datum: "XXX",
start: "XXX",
ziel: "XXX",
hinfahrt: "XXX",
ruckfahrt: "XXX",
zuzahlung: "XXX"
});
arr.push({
id: "6",
datum: "XXX",
start: "XXX",
ziel: "XXX",
hinfahrt: "XXX",
ruckfahrt: "XXX",
zuzahlung: "XXX"
});
arr.sort(function(a,b) {
if (a[0] === b[1]) {
return 0;
}
else {
return (a[0] < b[0]) ? -1 : 1;
}
});
console.log(arr);
I am struggling with using the sort function correctly. The desired output format should look like this:
{id: '3', datum: 'XXX', start: 'XXX', ziel: 'XXX', hinfahrt: 'on', …}
{id: '6', datum: 'XXX', start: 'XXX', ziel: 'XXX', hinfahrt: 'on', …}
{id: '9', datum: 'XXX', start: 'XXX', ziel: 'XXX', hinfahrt: 'on', …}