Created
September 14, 2017 21:10
-
-
Save brettbartylla/c5b219d610a81a45f34fe2b1d606e49d to your computer and use it in GitHub Desktop.
Sorts array values alphabetically using callback function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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