Module: Yahsort

Defined in:
lib/yahsort.rb,
lib/yahsort/helper.rb,
lib/yahsort/version.rb

Constant Summary collapse

PATTERN =

dig should indicate the ‘digits’ sec is secondary idx, number based match is done on same secondaries db1 api2 ==> api2, db1 prod0api1 —> secondary = prod0api, dig = 1

'^(?<sec>\w*\D)(?<dig>\d+)'
REGEX =
Regexp.new(PATTERN)
VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.fqdn_sorterObject



2
3
4
5
6
7
# File 'lib/yahsort/helper.rb', line 2

def self.fqdn_sorter
  pattern =  Yahsort::PATTERN + '\.'
  regex = Regexp.new(pattern)
  
  self.sorter(regex=regex)
end

.sorter(regex = REGEX, secondary_precedence = true) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yahsort.rb', line 12

def self.sorter(regex=REGEX, secondary_precedence=true)
  
  sorter = lambda do |x,y|
    reg = regex

    mX = reg.match(x)
    mY = reg.match(y)
    
    if (nil == mX) || (nil == mY)
      # fallback to default comparison
      return x <=> y
    end

    dX = mX[:dig].to_i
    secondaryX = mX[:sec]
    
    dY = mY[:dig].to_i
    secondaryY = mY[:sec]

    return dX <=> dY unless secondary_precedence

    #sort on digits if secondary is same
    if secondaryX == secondaryY
      return dX <=> dY
    else
      return x <=> y
    end
  end

  return sorter
end