This exercise involves taking an array of numbers and sorting them alphabetically. For example, [1,2,3,4] would return as [4,1,3,2]
I mapped the array first, returning an array of words, then sorted those words using Ruby's built-in sort function. Then, I mapped that array to return an array of integers.
It works, obviously. I want your suggestions and thoughts, different ideas and better ways I could do this in the future.
I know I could one-line the map functions, but thought this is better for readability. Example: alphabeticize = numbers.map {|num| alphas.fetch(:"#{num.to_s}")}.sort
def alphabetic_number_sort(numbers)
alphas = {"0": "zero", "1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine", "10": "ten", "11": "eleven", "12": "twelve", "13": "thirteen", "14": "fourteen", "15": "fifteen", "16": "sixteen", "17": "seventeen", "18": "eighteen", "19": "nineteen"}
alphabeticize = numbers.map do |num|
alphas.fetch(:"#{num.to_s}")
end.sort
sorted_alphabetically = alphabeticize.map do |word|
alphas.key(word).to_s.to_i
end
return sorted_alphabetically
end
p alphabetic_number_sort((0..19).to_a) == [
8, 18, 11, 15, 5, 4, 14, 9, 19, 1, 7, 17,
6, 16, 10, 13, 3, 12, 2, 0
]