Skip to content

Instantly share code, notes, and snippets.

@vladfaust
Created January 25, 2016 11:18
Show Gist options
  • Save vladfaust/a2e842bf75c4a6b37d32 to your computer and use it in GitHub Desktop.
Save vladfaust/a2e842bf75c4a6b37d32 to your computer and use it in GitHub Desktop.
Was given a playing cards sample array. The task was to sort it and make all the members unique.
@canonical_hash = Hash.new
def init
i = 0
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Aces' ].map do |value|
@canonical_hash[value] = (i += 1)
end
end
def card_sort (array)
array.uniq!
array.sort_by do |item|
@canonical_hash[item]
end
end
init
p "Sorted & unique: #{ card_sort([ 1, 'Queen', 'King', 'Jack', 3, 5, 7, 2, 2, 'Jack' ]) }"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment