Created
July 26, 2010 16:18
-
-
Save dekart/490786 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # categories - массив объектов Category с полем name, по которому идет алфивитная разбивка. | |
| # Сначала выводятся все буквенные названия, потом все остальное | |
| def categories_by_alphabet(categories) | |
| letter_sets = {} | |
| other = [] | |
| categories.each do |category| | |
| first_letter = category.name.mb_chars.downcase.first.to_s | |
| if first_letter =~ /[a-z]/i | |
| letter_sets[first_letter] ||= [] | |
| letter_sets[first_letter] << category | |
| else | |
| other << category | |
| end | |
| end | |
| result = letter_sets.to_a | |
| result.sort! | |
| result.collect!{|s| s.last } | |
| result.each do |letter| | |
| letter.sort!{|a, b| a.name <=> b.name } | |
| end | |
| result.unshift(other) if other.any? | |
| block_given? ? yield(result) : result | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment