class NaturalSort::Segment

Constants

NUMERIC

Segments that look like numbers, but start with 0 should be treated as strings, so that we don't asumme that 1.020 is more than 1.1 cause 20 > 1

Attributes

input[R]

Public Class Methods

new(input) click to toggle source
# File lib/natural_sort/segment.rb, line 12
def initialize(input)
  @input = input.to_s
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/natural_sort/segment.rb, line 20
def <=>(other)
  if numeric? && other.numeric?
    Rational(input) <=> Rational(other.to_s)
  else
    compare_chars(input, other.to_s)
  end
end
numeric?() click to toggle source
# File lib/natural_sort/segment.rb, line 28
def numeric?
  NUMERIC === input
end
to_s() click to toggle source
# File lib/natural_sort/segment.rb, line 16
def to_s
  @input
end

Private Instance Methods

compare_chars(a, b) click to toggle source
# File lib/natural_sort/segment.rb, line 34
def compare_chars(a, b)
  a == b.swapcase ? a <=> b : a.downcase <=> b.downcase
end