Skip to content

Instantly share code, notes, and snippets.

@brettbartylla
Created September 14, 2017 21:10
Show Gist options
  • Save brettbartylla/c5b219d610a81a45f34fe2b1d606e49d to your computer and use it in GitHub Desktop.
Save brettbartylla/c5b219d610a81a45f34fe2b1d606e49d to your computer and use it in GitHub Desktop.
Sorts array values alphabetically using callback function
var myArr = [{
num: 5,
str:'apple'
},{
num: 7,
str:'cabbage'
},{
num: 1,
str:'ban'
}];
myArr.sort(function(val1, val2){
//sorts array alphabetically
if(val1.str < val2.str){
return -1;
}
else{
return 1;
}
});
console.log(myArr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment