I'm trying to sort the next array alphabetically by the data keys:
array = [
{index: 1,
data: "d"
},
{index: 2,
data: "c"
},
{index: 3,
data: "a"
},
{index: 4,
data: "f"
},
{index: 5,
data: "e"
},
{index: 6,
data: "b"
}
];
such that it will become as follows:
array = [
{index: 3,
data: "a"
},
{index: 6,
data: "b"
},
{index: 2,
data: "c"
},
{index: 1,
data: "d"
},
{index: 5,
data: "e"
},
{index: 4,
data: "f"
}
];
Already found and tried something like this:
array.sort(function (a, b) { return a[1] - b[1]; });
but with no success. Is there a simple way to achieve this? Pure js please.