Class: NaturalSortJp::Element

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/natural_sort_jp/element.rb

Constant Summary collapse

SORT_PRIORITY =
{ int: 1, str: 2 }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Element

Returns a new instance of Element.



9
10
11
# File 'lib/natural_sort_jp/element.rb', line 9

def initialize(val)
  @val = zenkaku2hankaku(val)
end

Instance Method Details

#<=>(other) ⇒ Object



13
14
15
# File 'lib/natural_sort_jp/element.rb', line 13

def <=>(other)
  to_array <=> other.to_array
end

#to_arrayObject



17
18
19
20
21
22
23
# File 'lib/natural_sort_jp/element.rb', line 17

def to_array
  if @val =~ /^(\p{Digit}+$)/
    [SORT_PRIORITY[:int], @val.to_i]
  else
    [SORT_PRIORITY[:str], @val]
  end
end