Skip to content

Instantly share code, notes, and snippets.

@omgreenfield
Created August 13, 2021 21:09
Show Gist options
  • Save omgreenfield/177086f8fa87df80632de5575d13fbd8 to your computer and use it in GitHub Desktop.
Save omgreenfield/177086f8fa87df80632de5575d13fbd8 to your computer and use it in GitHub Desktop.
Finding the higher version between two objects
objects = [
# This is the higher version
{ name: "thing1", version: "5.15.6" },
# but string comparison would think this one is
{ name: "thing2", version: "5.6.1" },
]
objects.sort do |a, b|
# The comparison operator works as you'd expect on arrays of numbers
a_comps = a[:version].split(".").map(&:to_i)
b_comps = b[:version].split(".").map(&:to_i)
a_comps <=> b_comps
end.last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment