Skip to content

Instantly share code, notes, and snippets.

@tareksamni
Last active September 28, 2017 19:55
Show Gist options
  • Save tareksamni/99b85edc87155d69ebf41980ce1e8336 to your computer and use it in GitHub Desktop.
Save tareksamni/99b85edc87155d69ebf41980ce1e8336 to your computer and use it in GitHub Desktop.
Yelp online question
# Input:
businesses = [
{ rating: 1, id: 1000 },
{ rating: 4, id: 1020 },
{ rating: 5, id: 1300 },
{ rating: 5, id: 1040 },
{ rating: 9, id: 1005 },
{ rating: 2, id: 1104 },
{ rating: 3, id: 1400 }
]
# Solution:
businesses.sort_by.with_index { |item, idx| [-item[:rating], idx] }
# output:
# businesses = [
# { rating: 9, id: 1005 },
# { rating: 5, id: 1300 },
# { rating: 5, id: 1040 },
# { rating: 4, id: 1020 },
# { rating: 3, id: 1400 },
# { rating: 2, id: 1104 },
# { rating: 1, id: 1000 }
# ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment