class EverydayNatsort::Natural
Constants
- NUMERIC
- REGEXP
Public Class Methods
both_exist?(it, ma, mb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 52 def both_exist?(it, ma, mb) ma[it] && mb[it] end
both_have_group_1?(it, ma, mb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 56 def both_have_group_1?(it, ma, mb) ma[it][1] && mb[it][1] end
both_num?(it, ma, mb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 60 def both_num?(it, ma, mb) num?(ma[it][0]) && num?(mb[it][0]) end
check_equal(ret)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 77 def check_equal(ret) (ret[0] <=> ret[1]) == 0 end
compare(a, b)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 17 def compare(a, b) sa, sb = a.to_s, b.to_s if (sa.downcase <=> sb.downcase) == 0 sa <=> sb else na, nb = nat_sanatize(sa, sb) na <=> nb end end
format(match_data, length)
click to toggle source
format([a, 1], 3) => a001 add leading zero
# File lib/everyday_natsort/natural.rb, line 83 def format(match_data, length) match_data[1].gsub(/-|_/, '').downcase + ("%0#{length}d" % match_data[2].to_i) end
match_num?(it, ma, mb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 48 def match_num?(it, ma, mb) both_exist?(it, ma, mb) && both_have_group_1?(it, ma, mb) && both_num?(it, ma, mb) end
multireg(regpexp, str)
click to toggle source
return an array with regexp matchdata on str
# File lib/everyday_natsort/natural.rb, line 89 def multireg(regpexp, str) result = [] while regpexp.match(str) result.push regpexp.match(str) str = regpexp.match(str).post_match end result end
nat_sanatize(sa, sb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 27 def nat_sanatize(sa, sb) sa = EverydayNatsort::Accents.sanitize(sa).gsub('-', '_') sb = EverydayNatsort::Accents.sanitize(sb).gsub('-', '_') ma, mb = multireg(REGEXP, sa), multireg(REGEXP, sb) ret = sanatize_loop(ma, mb) return ret[0], ret[1] end
num?(v)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 73 def num?(v) NUMERIC.match(v) end
process_alpha_match(it, ma, mb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 69 def process_alpha_match(it, ma, mb) [ma[it][0].downcase, mb[it][0].downcase] end
process_match(it, ma, mb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 44 def process_match(it, ma, mb) match_num?(it, ma, mb) ? process_numeric_match(it, ma, mb) : process_alpha_match(it, ma, mb) end
process_numeric_match(it, ma, mb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 64 def process_numeric_match(it, ma, mb) l = [ma[it][2].size, mb[it][2].size].max [format(ma[it], l), format(mb[it], l)] end
sanatize_loop(ma, mb)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 35 def sanatize_loop(ma, mb) ret = ['', ''] (0...[ma.size, mb.size].min).each { |it| ret = process_match(it, ma, mb) break unless check_equal(ret) } ret end
sort(object)
click to toggle source
# File lib/everyday_natsort/natural.rb, line 11 def sort(object) Array(object).sort do |a, b| self.compare(a, b) end end