Skip to content

Instantly share code, notes, and snippets.

@dekart
Created July 26, 2010 16:18
Show Gist options
  • Select an option

  • Save dekart/490786 to your computer and use it in GitHub Desktop.

Select an option

Save dekart/490786 to your computer and use it in GitHub Desktop.
# 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