Skip to content

Instantly share code, notes, and snippets.

@Thaekeh
Last active January 31, 2023 22:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thaekeh/45e365402b9ff124698ba208d76260e3 to your computer and use it in GitHub Desktop.
Save Thaekeh/45e365402b9ff124698ba208d76260e3 to your computer and use it in GitHub Desktop.
Sorting an array numerically
computed: {
sortedArray() {
let sortedRecipes = this.recipes;
if (sortMethod == 'alphabetically' {
sortedRecipes = sortedRecipes.sort((a,b) => {
let fa = a.title.toLowerCase(), fb = b.title.toLowerCase();
if (fa < fb) {
return -1
}
if (fa > fb) {
return 1
}
return 0
})
} else if (sortMethod == 'cookingTime' {
sortedRecipes = sortedRecipes.sort((a,b) => {
return a.cookingTime - b.cookingTime
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment