Created
July 5, 2017 15:37
-
-
Save thibaultboursier/509bddde6df39142a1febd16f20f8a98 to your computer and use it in GitHub Desktop.
Sort object collection by property's value
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
arr.reduce((previous, current, index) => { | |
var obj = previous; | |
var currentValue = current.label.charCodeAt(0); | |
if (obj.lastValue && obj.lastValue > currentValue) { | |
obj.sort.splice(index - 1, 0, current); | |
} else { | |
obj.sort.push(current); | |
} | |
obj.lastValue = currentValue; | |
return obj; | |
}, {sort:[], lastValue:null}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment