T
Taylor Strait
I am using case in to determine the sorting method used for an array as
so:
def single_choice(object)
# sort answer order based on question.list_method
answers = object.answers
sorted_answers = case object.list_method
when 'position' then answers.sort_by {|answer| answer.position}
when 'random' then answers.sort_by {rand}
when 'alphabetical' then answers.sort_by {|answer| answer.text}
when 'reverse_alpha' then answers.sort_by {|answer| answer.text}
return answers.reverse
when 'shortest_first' then answers.sort_by {|answer|
answer.text.length}
when 'longest_first' then answers.sort_by {|answer|
answer.text.length}
return answers.reverse
end
This does not because of the return statements. However, I need to
reverse the result for those two options. How can I do this with case
statements? Or must I use if..elsif?
so:
def single_choice(object)
# sort answer order based on question.list_method
answers = object.answers
sorted_answers = case object.list_method
when 'position' then answers.sort_by {|answer| answer.position}
when 'random' then answers.sort_by {rand}
when 'alphabetical' then answers.sort_by {|answer| answer.text}
when 'reverse_alpha' then answers.sort_by {|answer| answer.text}
return answers.reverse
when 'shortest_first' then answers.sort_by {|answer|
answer.text.length}
when 'longest_first' then answers.sort_by {|answer|
answer.text.length}
return answers.reverse
end
This does not because of the return statements. However, I need to
reverse the result for those two options. How can I do this with case
statements? Or must I use if..elsif?