Ordered Hash (v. 1.8)

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

···

--
Posted via http://www.ruby-forum.com/.

1. You could use the RBTree gem.

2. Iterate through the sorted keys when you need to access elements
    in sorted order:

1.9.3p194 :001 > hash = {:c => 1, :a => 2, :b => 3}
  => {:c=>1, :a=>2, :b=>3}
1.9.3p194 :002 > sorted_keys = hash.keys.sort
  => [:a, :b, :c]
1.9.3p194 :003 > sorted_keys.each do |key|
1.9.3p194 :004 > puts "#{key} => #{hash[key]}"
1.9.3p194 :005?> end
a => 2
b => 3
c => 1
  => [:a, :b, :c]

···

Am 30.11.2012 02:46, schrieb Doug Jolley:

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

--
<https://github.com/stomar/&gt;

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 + "ook"}

h = Hash[h.sort]
p h #> {"b"=>"book", "c"=>"cook", "h"=>"hook", "l"=>"look", "t"=>"took"}

Harry

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 + "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"}

Harry

You can use Hashery's Disctionary class.

  $ gem install hashery

Then code:

  require 'hashery/dictionary'

  dict = Hashery::Dictionary.new_by{ |k,v| k }

···

On Thursday, November 29, 2012 8:46:48 PM UTC-5, Doug Jolley 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.

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.

      ... doug

···

--
Posted via http://www.ruby-forum.com/.

correction: in 1.8, when your keys are symbols you need to sort with

   sorted_keys = hash.keys.sort_by(&:to_s)

···

Am 30.11.2012 07:15, schrieb sto.mar@web.de:

Am 30.11.2012 02:46, schrieb Doug Jolley:

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

1. You could use the RBTree gem.

2. Iterate through the sorted keys when you need to access elements
    in sorted order:

1.9.3p194 :001 > hash = {:c => 1, :a => 2, :b => 3}
  => {:c=>1, :a=>2, :b=>3}
1.9.3p194 :002 > sorted_keys = hash.keys.sort
  => [:a, :b, :c]
1.9.3p194 :003 > sorted_keys.each do |key|
1.9.3p194 :004 > puts "#{key} => #{hash[key]}"
1.9.3p194 :005?> end
a => 2
b => 3
c => 1
  => [:a, :b, :c]

--
<https://github.com/stomar/&gt;

not in Ruby 1.8!

And in 1.9 you would have to recreate the complete hash for
each key-value pair that is added.

···

Am 30.11.2012 07:19, schrieb Harry Kakueki:

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 + "ook"}

h = Hash[h.sort]
p h #> {"b"=>"book", "c"=>"cook", "h"=>"hook", "l"=>"look", "t"=>"took"}

--
<https://github.com/stomar/&gt;

This does not work in 1.8, because hashes in 1.8 do
*not* keep the insertion order.

···

Am 30.11.2012 08:09, schrieb Harry Kakueki:

# 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

--
<https://github.com/stomar/&gt;

You can access the elements in sorted order even more easily like this:

   hash = {:c => 1, :a => 2, :b => 3}

   hash.sort_by(&:to_s).each do |k, v|
     puts "#{k} => #{v}"
   end

output:

a => 2
b => 3
c => 1

···

Am 30.11.2012 07:22, schrieb sto.mar@web.de:

Am 30.11.2012 07:15, schrieb sto.mar@web.de:

Am 30.11.2012 02:46, schrieb Doug Jolley:

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

--
<https://github.com/stomar/&gt;

That's why Doug had installed the OrderedHash Gem I guess. So with a
little change that should work:

# untested:
OrderedHash[*sort.flatten]

Kind regards

robert

···

On Fri, Nov 30, 2012 at 3:34 PM, <sto.mar@web.de> wrote:

Am 30.11.2012 08:09, schrieb Harry Kakueki:

# 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

This does not work in 1.8, because hashes in 1.8 do
*not* keep the insertion order.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/