Method: Enumerable#smart_sort
- Defined in:
- lib/initializers/enumerable_changes.rb
#smart_sort ⇒ Object
Sorts all items in this enumerable first by non-digits and then by digits.
If a block is given, it will be invoked to calculate the basis for comparison. Otherwise, the items themselves will be used as the basis for comparison.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/initializers/enumerable_changes.rb', line 10 def smart_sort # :yields: item sort_by do |item| basis = if block_given? yield item else item end.to_s alpha = basis.scan(/\D+/) digit = basis.scan(/\d+/).map(&:to_i) [alpha, digit] end end |