dimanche 6 juillet 2014

Sort hash having duplicate values in ascending order of keys


Vote count:

0




I have a hash that looks like this -



{"P1"=>108, "P6"=>50, "P3"=>50, "P5"=>40, "P2"=>40, "P4"=>40}


This is already sorted in the descending order of value.


I want the below output, wherein the original ordering(by value descending) is maintained, but for those keys that have the same values need to be sorted in ascending order of keys(for e.g., p3 comes before p6). I did have a look at the Hashes documentation and also googled around quite a bit, but I'm unable to find an easy way of doing this right with the approach that I could think of.



{"P1"=>108, "P3"=>50, "P6"=>50, "P2"=>40, "P4"=>40, "P5"=>40 }


Any pointers of approaching this in a simple way through ruby would be helpful.


Thank you.



asked 32 secs ago






Aucun commentaire:

Enregistrer un commentaire