I am running Ruby v. 1.8.7 and I have installed the orderedhash gem. I
know that the purpose of orderedhash is to preserve the order of
creation. However, what I’d really like to do is, after the hash has
been created, sort it on the key in a manner similar to the way that I
would sort an array with the sort!() method and then preserve the sorted
order. Any ideas on how I might accomplish that feat? Thanks for any
input.
I am running Ruby v. 1.8.7 and I have installed the orderedhash gem. I
know that the purpose of orderedhash is to preserve the order of
creation. However, what I’d really like to do is, after the hash has
been created, sort it on the key in a manner similar to the way that I
would sort an array with the sort!() method and then preserve the sorted
order. Any ideas on how I might accomplish that feat? Thanks for any
input.
... doug
You could use the RBTree gem.
Iterate through the sorted keys when you need to access elements
in sorted order:
creation. However, what I’d really like to do is, after the hash has
been created, sort it on the key in a manner similar to the way that I
would sort an array with the sort!() method and then preserve the sorted
order. Any ideas on how I might accomplish that feat? Thanks for any
input.
... doug
Does this work?
h = {}
arr = [“t”,“c”,“h”,“b”,“l”]
arr.each{|x| h[x] = x + “ook”}
h = Hash[h.sort]
p h #> {“b”=>“book”, “c”=>“cook”, “h”=>“hook”, “l”=>“look”,
“t”=>“took”}
I am running Ruby v. 1.8.7 and I have installed the orderedhash gem. I
know that the purpose of orderedhash is to preserve the order of
creation. However, what I’d really like to do is, after the hash has
been created, sort it on the key in a manner similar to the way that I
would sort an array with the sort!() method and then preserve the sorted
order. Any ideas on how I might accomplish that feat? Thanks for any
input.
... doug
If you want to also keep the original order, maybe this is worth a
look.
I’m using 1.9 so I can’t test it for your setup.
class Hash
def myway
Hash[*sort.flatten] #Ruby 1.8 ? #Hash[sort] # Ruby1.9
end
end
h = {}
arr = [“t”,“c”,“h”,“b”,“l”]
arr.each{|x| h[x] = x + “ook”}
p h #> {“t”=>“took”, “c”=>“cook”, “h”=>“hook”,
“b”=>“book”, “l”=>“look”}
p h.myway #> {“b”=>“book”, “c”=>“cook”, “h”=>“hook”, “l”=>“look”,
“t”=>“took”}
Thanks to all who contributed in this thread. Using your valuable
contributions, I was able to solve my problem. The final solution was
not at all what I had in mind when I posted my original post. Thanks to
the guidance obtained from this thread, I was able to accomplish my
objective by using the sort method of a traditional hash (i.e., I did
not need the ordered hash as I thought that I would). Thanks for
getting me pointed in the right direction.
On Thursday, November 29, 2012 8:46:48 PM UTC-5, Doug J. wrote:
I am running Ruby v. 1.8.7 and I have installed the orderedhash gem. I
know that the purpose of orderedhash is to preserve the order of
creation. However, what I’d really like to do is, after the hash has
been created, sort it on the key in a manner similar to the way that I
would sort an array with the sort!() method and then preserve the sorted
order. Any ideas on how I might accomplish that feat? Thanks for any
input.
You can use Hashery’s Disctionary class.
$ gem install hashery
Then code:
require ‘hashery/dictionary’
dict = Hashery::Dictionary.new_by{ |k,v| k }
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.