Created
September 23, 2022 09:25
-
-
Save lukad/868ba93214dda24157a7e893522c092a to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
array = [ | |
'Selfie Stick,98,29', | |
'iPhone Case,90,15', | |
'Fire TV Stick,48,49', | |
'Wyze Cam,48,25', | |
'Water Filter,56,49', | |
'Blue Light Blocking Glasses,90,16', | |
'Ice Maker,47,119', | |
'Video Doorbell,47,199', | |
'AA Batteries,64,12', | |
'Disinfecting Wipes,37,12', | |
'Baseball Cards,73,16', | |
'Winter Gloves,32,112', | |
'Microphone,44,22', | |
'Pet Kennel,5,24', | |
'Jenga Classic Game,100,7', | |
'Ink Cartridges,88,45', | |
'Instant Pot,98,59', | |
'Hoze Nozzle,74,26', | |
'Gift Card,45,25', | |
'Keyboard,82,19' | |
] | |
array = array.map do |item| | |
name, popularity, price = item.split(',') | |
[name, popularity.to_i, price.to_i] | |
end | |
array = array.sort do |(_, popularity_a, price_a), (_, popularity_b, price_b)| | |
comparison = popularity_b <=> popularity_a | |
next price_a <=> price_b if comparison.zero? | |
comparison | |
end | |
array.each do |item| | |
puts item.join(',') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment