Skip to content

Instantly share code, notes, and snippets.

@bcardiff
Last active October 26, 2020 20:05
Show Gist options
  • Save bcardiff/5395bfaef35ac1d14a1a to your computer and use it in GitHub Desktop.
Save bcardiff/5395bfaef35ac1d14a1a to your computer and use it in GitHub Desktop.
data = [{"Brian", 42}, {"Alan", 30}, {"Brian", 32}]
# unstable
data.sort_by &.[0] # => [{"Brian", 42}, {"Alan", 30}, {"Brian", 32}]
# stable
data.map_with_index { |d,i| {d,i} }.sort_by { |di| {di[0][0], di[1]} }.map { |di| di[0] } # => [{"Alan", 30}, {"Brian", 42}, {"Brian", 32}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment