Skip to content

Instantly share code, notes, and snippets.

@paulgrammer
Created August 23, 2021 18:58
Show Gist options
  • Save paulgrammer/6818238c51e0d15d67488dcf54fc1699 to your computer and use it in GitHub Desktop.
Save paulgrammer/6818238c51e0d15d67488dcf54fc1699 to your computer and use it in GitHub Desktop.
function sortArray(values) {
return values.sort(function (a, b) {
let authorChuckedA = a.author.split(" ");
let authorChuckedB = b.author.split(" ");
let lastNameA = authorChuckedA[authorChuckedA.length - 1];
let lastNameB = authorChuckedB[authorChuckedB.length - 1];
return lastNameA.localeCompare(lastNameB);
});
}
let sorted = sortArray([
{ name: "Harry Potter", rating: "8+", author: "Joanne Rowling" },
{ name: "Warcross", rating: "13+", author: "Marie Lu" },
{ name: "The Hunger Games", rating: "12+", author: "Suzanne Collins" },
]);
console.log(sorted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment