I make my hash in the order listed below and I sort it in ascending order fine, but when I want to sort it in descending order I don’t get the correct result.
Hash is this
hash_answers = {}
unless answers.blank?
answers.each_with_index do |ans, index|
voted_up_users = ans.votes_up_by_all_users(ans)
voted_down_users = ans.votes_down_by_all_users(ans)
hash_answers[ans.id] = {
:id => ans.id,
:count => voted_up_users.count - voted_down_users.count,
:created_at => ans.created_at
}
end
end
This code above works fine when I sort it in ascending order by created_at and count base
answers = hash_answers.sort_by { |k, v| [ v[:count] , v[:created_at] ] }
but how can I sort this hash in descending order by created_at and on base of count.
Help if anyone can
Thanks
You can use block in sort method, in block you should provide the sort rule, in your case it might be like this: