I’d think this would be easy, and have searched for this pretty hard, but can’t seem to get it to work.
I have the following hash:
@friends = [{"name"=>"John Smith", "id"=>"12345"}, {"name"=>"Jane Doe", "id"=>"23456"}, {"name"=>"Samuel Jackson", "id"=>"34567"}, {"name"=>"Kate Upton", "id"=>"45678"}]
I’m trying to sort it alphabetically by the name.
Right now I”m doing this:
@friends.sort{|a,b| a[0]<=>b[0]}
However, it just outputs the full results in non-alphabetical order.
The problem is that a and b are Hash, so you have to use “name” as the key or index instead of 0. So this should do it
Also remember to use sort! to modify @friends variable or set it to the result
or