I'm trying to sort an array of comment objects by their "body" attribute.
I'm trying to run the following (console.log(comment) successfully shows the array) but when I go to sort it, I just get the same array back - even after defining it as a sortedArray
variable.
I've seen some similar questions, but not quite with the arrow function syntax that I'm trying to implement.
Below is my code:
function sortComments() {
$("#sort_comments").on("click", function(e) {
e.preventDefault();
var id = this.dataset.linkid
fetch(`/links/${id}/comments.json`)
.then(r => r.json())
.then(comments =>
comments.sort(function(a, b) {
return a.body - b.body;
})
);
});
}
Thank you for your help.