ruby - Alphabetize a list by the nth character -
write function accepts 2 parameters, i) string (containing list of words) , ii) integer (n). function should alphabetize list based on nth letter of each word.
i have tried
def sort_it(list_, n) list_.sort_by {|name| name[n]} end but saying sort_by not recognised.
is there elegant way solve this?
list_ string while sort_by method of enumerable. need convert string collection of words before sorting. 1 way is
list_.split
so code
def sort_it(list_, n) list_.split.sort_by {|name| name[n]} end as side note, don't use trailing underscore in argument name.
Comments
Post a Comment