Skip to content

Instantly share code, notes, and snippets.

@J-Scag
Created October 11, 2013 15:33
Show Gist options
  • Select an option

  • Save J-Scag/6936811 to your computer and use it in GitHub Desktop.

Select an option

Save J-Scag/6936811 to your computer and use it in GitHub Desktop.
Version sort TODO for Flatiron School day 7
filenames = [
"foo-1.10.2.ext",
"foo-1.11.ext",
"foo-1.3.ext",
"foo-1.50.ext",
"foo-1.8.7.ext",
"foo-1.9.3.ext",
"foo-1.ext",
"foo-10.1.ext",
"foo-10.ext",
"foo-100.ext",
"foo-13.ext",
"foo-2.0.0.ext",
"foo-2.0.1.ext",
"foo-2.0.ext",
"foo-2.007.ext",
"foo-2.01.ext",
"foo-2.012b.ext",
"foo-2.01a.ext",
"foo-2.0a.ext",
"foo-2.0b.ext",
"foo-2.1.ext",
"foo-25.ext",
"foo-6.ext",
]
version_sorted_filenames = [
"foo-1.ext",
"foo-1.3.ext",
"foo-1.8.7.ext",
"foo-1.9.3.ext",
"foo-1.10.2.ext",
"foo-1.11.ext",
"foo-1.50.ext",
"foo-2.0.ext",
"foo-2.0a.ext",
"foo-2.0b.ext",
"foo-2.0.0.ext",
"foo-2.0.1.ext",
"foo-2.01.ext",
"foo-2.1.ext",
"foo-2.01a.ext",
"foo-2.007.ext",
"foo-2.012b.ext",
"foo-6.ext",
"foo-10.ext",
"foo-10.1.ext",
"foo-13.ext",
"foo-25.ext",
"foo-100.ext",
]
# class Array
# def version_sort
# sorted = self
# sorted.sort_by do |item|
# temp = (/\d+$/.match(item.split('.')[0]))
# puts temp
# temp.to_s.to_i
# end
# sorted.sort_by do |item|
# temp = (/\d+$/.match(item.split('.')[1]))
# puts temp
# temp.to_s.to_i
# end
# end
# end
class Array
def version_sort
self.sort_by do |file|
file.scan(/\d+|[a-z]*/).map { |el| el.to_i }
end
end
end
puts filenames.version_sort == version_sorted_filenames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment