Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/dictionary_order_sort/array.rb

Instance Method Summary collapse

Instance Method Details

#dict_sortObject

Sorts an array of strings with ‘dictionary sort’ logic. Produces the same order as the Linux command ‘sort –dictionary-order`. (Note that --dictionary-order is the default order for the sort command.)



5
6
7
8
9
10
11
12
13
14
# File 'lib/dictionary_order_sort/array.rb', line 5

def dict_sort
  sorting_cache = {}
  sort do |a, b|
    c, d = [a, b].map do |el|
      str = el.to_s
      sorting_cache[str] ||= str.gsub /\W|[_\s]/, ''
    end
    c <=> d
  end
end