ruby/vers.rb: sort version numbers in ascending order

This commit is contained in:
Nick Chambers 2021-07-25 20:27:29 -05:00
parent 0f8fd8a9b6
commit 9e16541e36
1 changed files with 30 additions and 0 deletions

30
ruby/vers.rb Normal file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env ruby
def sort_ver(vers)
vers.sort do |v1, v2|
v1_info = v1.split(?.).map &:to_i
v2_info = v2.split(?.).map &:to_i
ord_found = 0
while ord_found.zero? and not v1_info.empty? and not v2_info.empty?
ord_found = v2_info.first <=> v1_info.first
v1_info.shift
v2_info.shift
end
if ord_found.zero?
v2_info.size <=> v1_info.size
else
ord_found
end
end
end
vers = [
"0.1.0", "3.2.1", "2.2.3",
"3.2", "5.0", "5.0.1",
"0.1.1", "0.1", "4.2.0",
"17.9.2", "4.20", "18.7.3"
]
p vers, sort_ver(vers)