Created
August 13, 2021 21:09
-
-
Save omgreenfield/177086f8fa87df80632de5575d13fbd8 to your computer and use it in GitHub Desktop.
Finding the higher version between two objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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