I have a hash like this (it is the result of the group_by method):
{nil => [#<ActiveRecord id ..., ...], #<ActiveRecord ...> => [#<ActiveRecord id ..., ...], ...}
I need to sort it so that the nil elements would be the first, and then all the other ActiveRecord objects. How can I do this?
Thanx
P.S.
- Yes, I need an ActiveRecord objects as the keys (not symbols or some else)
- I can't do the order in my DB due to complex SQL request.
I need only to sort the hash by the keys.
hash = { nil=> ["dog", "cat"], "pig"=>["zebra", "sloth"], "groundhog"=>["toad", "ladybug"] }(though I didn't read your question closely enough to know if that would make sense). I see in your comment to @Stefan's answer that you are sorting on string representations ofActiveRecordobjects. I don't think those answering were expecting you wanted to do that, because such objects do not respond to<=>.