Sorting objects by alphabet in Ruby 1.8. Use custom alphabet or at least accent-stripping fallback.
Switch branches/tags
Nothing to show
Latest commit 1ad5fc6 Jun 12, 2012 @moskyt some 1.9ism
Failed to load latest commit information.
lib some 1.9ism Jun 12, 2012
test 0.2.1 : fallbacks for accents Jan 23, 2010
.gitignore gitignore Jan 19, 2010
CHANGELOG first commit Jan 19, 2010
MIT-LICENSE first commit Jan 19, 2010
init.rb some 1.9ism Jun 12, 2012
sort-by-alphabet.gemspec some 1.9ism Jun 12, 2012


This library implements alphabetical sorting according to given alphabet scheme. Currently, only Czech alphabet is included, but it is super-easy to create a new one.


Install the gem:

gem install sort-by-alphabet

Or require it into Rails:

gem require "sort-by-alphabet"


Support of mb_chars, i.e. ActiveSupport in practice.


Classical sort puts accented chars after normal:

%w{nazdar ahoj čau}.sort # => %w{ahoj nazdar čau}

sort_by_alphabet does a better job:

%w{nazdar ahoj čau}.sort_by_alphabet(CzechAlphabet) # => %w{ahoj čau nazdar}

sort_by_alphabet can take a block which acts similarly as that in sort_by:

Something.all.sort_by_alphabet(CzechAlphabet) {|o| o.title}

You can also make a class alphabetically comparable, enabling sorting:

class Something

a =
b =
c =

# the following comparisons will be using to_s and lexical order given by Czech alphabet

a <=> b

[a, b, c].sort 

or use a block, e.g. for case insensitivity:

class SomethingElse
  comparable_by_alphabet(CzechAlphabet) {|obj| obj.mb_chars.downcase}