sorting - Sort hash by key, return hash in Ruby -
would best way sort hash , return hash object (instead of array):
h = {"a"=>1, "c"=>3, "b"=>2, "d"=>4} # => {"a"=>1, "c"=>3, "b"=>2, "d"=>4} hash[h.sort] # => {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
in ruby 2.1 simple:
h.sort.to_h
Comments
Post a Comment