Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thibaultboursier/509bddde6df39142a1febd16f20f8a98 to your computer and use it in GitHub Desktop.
Save thibaultboursier/509bddde6df39142a1febd16f20f8a98 to your computer and use it in GitHub Desktop.
Sort object collection by property's value
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