1) I’ve a hash, coding in ruby console
influencerHash
influencerHash.class => Hash
{"inf3"=>{"followingCount"=>256, "followersCount"=>80, "name"=>"Branislav Seslija", "score"=>10.4099998474121}, "inf2"=>{"followingCount"=>6, "followersCount"=>4, "name"=>"Greg Seslija", "score"=>29.8400001525879}, "inf1"=>{"followingCount"=>13, "followersCount"=>10, "name"=>"Amit Kumar", "score"=>30.6499996185303}}
2) I sorted it but the Hash is converted to Array
sortHash = influencerHash.sort
sortHash.class => Array
[["inf1", {"followingCount"=>13, "followersCount"=>10, "name"=>"Amit Kumar", "score"=>30.6499996185303}], ["inf2", {"followingCount"=>6, "followersCount"=>4, "name"=>"Greg Seslija", "score"=>29.8400001525879}], ["inf3", {"followingCount"=>256, "followersCount"=>80, "name"=>"Branislav Seslija", "score"=>10.4099998474121}]]
3) I converted it back to hash but result of sorting is reversed ( see above array and below hash result )
sortHash = Hash[influencerHash.sort]
sortHash.class => Hash
{"inf3"=>{"followingCount"=>256, "followersCount"=>80, "name"=>"Branislav Seslija", "score"=>10.4099998474121}, "inf2"=>{"followingCount"=>6, "followersCount"=>4, "name"=>"Greg Seslija", "score"=>29.8400001525879}, "inf1"=>{"followingCount"=>13, "followersCount"=>10, "name"=>"Amit Kumar", "score"=>30.6499996185303}}
**
Why this happened and how will i get same sorting that was in Array but as a Hash ??
**
It appears that you are using a version of Ruby pre-1.9, where there is no concept of the elements of a Hash having an order.
If you really need ordered Hashes, and if you really can’t upgrade to Ruby 1.9, then there are libraries available that implement ordered Hashes for older versions of Ruby. (E.g. Hashery, among many others.)
If at all possible, I’d recommend upgrading to Ruby 1.9.