2

Want to sort an array like this:

arr = ['4.1.5', '4.1.1', '4.1.2', '10.1']

Expected:

['4.1.1', '4.1.2', '4.1.5', '10.1']

Got:

["10.1", "4.1.1", "4.1.2", "4.1.5"]

What I've tried:

arr.sort(&:to_i) and arr.sort(&:to_f) but none of them handle the double dots

Ruslan
  • 1,919
  • 21
  • 42
  • Or you can use a third party library, e.g. [this](https://github.com/github/version_sorter). – Tom Lord Feb 01 '18 at 18:01
  • 4
    The answers in the other question are a bit more complex, here is a simpler solution for this problem. arr.sort_by {|e| e.split('.').map(&:to_i)} – Nick Ellis Feb 01 '18 at 18:33
  • `arr.sort_by { |v| Gem::Version.new(v) }` is not too complex – omikes Feb 02 '18 at 00:59

0 Answers0