Last active
December 15, 2015 13:58
-
-
Save imevro/5271006 to your computer and use it in GitHub Desktop.
Алфавитная сортировка с разделением в объекты по первой букве
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
function sortByLetter( array ) { | |
var length = array.length, | |
groups = {}, | |
item, | |
firstChar; | |
for ( var i = 0; i < length; i ++ ) { | |
item = array[ i ], | |
firstChar = item.charAt( 0 ); | |
groups[ firstChar ] = groups[ firstChar ] || []; | |
groups[ firstChar ].push( item ); | |
} | |
return groups; | |
} |
Обновил для лучшей производительности.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
via http://stackoverflow.com/questions/6957824/js-loop-through-strings-find-first-letter-output-as-letter-group-anchor-to-as