Class: Ned::Sort

Inherits:
Command show all
Defined in:
lib/ned/commands/sort.rb

Constant Summary collapse

DIGIT_MATCH =
/^ *((?:0|-?[1-9][0-9]*)(?:[.][0-9]+)?)(.*)/

Instance Method Summary collapse

Methods inherited from Command

config, description, #execute, #execute_all, help, #initialize, #load_lines, long_name, option_parser, #options, #parse, #peek, require_all, #require_all, require_all?, #require_all?, short_name

Constructor Details

This class inherits a constructor from Ned::Command

Instance Method Details

#execute_internal(lines) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ned/commands/sort.rb', line 32

def execute_internal(lines)
  lines[-1].ensure_trailing_newline if lines[-1]

  if options.fetch(:numeric, false)
    sortable = lines.map do |line|
      match = line.match(DIGIT_MATCH)
      if match
        [0, Float(match[1]), match[2], line]
      else
        [1, nil, nil, line]
      end
    end
    lines = sortable.sort.map { |v| v[3] }
  else
    lines.sort!
  end

  lines.reverse! if options.fetch(:reverse, false)
  lines
end