Skip to content

Instantly share code, notes, and snippets.

@Elizabella
Forked from alejandrolechuga/sorting-arrays.js
Created July 9, 2021 05:44
Show Gist options
  • Save Elizabella/609f72c19404237fcd5a0b7e94fffaf7 to your computer and use it in GitHub Desktop.
Save Elizabella/609f72c19404237fcd5a0b7e94fffaf7 to your computer and use it in GitHub Desktop.
// sort
var pokemons = [
{ name:'pickachu', level: 50 } ,
{ name:'bulbasour', level: 10 } ,
{ name:'snorlax', level: 1 } ,
{ name:'mew', level: 2 } ,
{ name:'abra', level: 150 }
];
// var numbers = [2, 34, 10, 2, 5, 1];
console.log(pokemons.sort(function (prev, next) {
if (prev.name > next.name) {
return 1;
}
if (prev.name < next.name) {
return -1;
}
return 0;
// return prev.level - next.level;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment