From: "Ara.T.Howard" Date: 2004-03-03T15:34:45+09:00 Subject: Re: why are Hashes so unsorted? what's your solution? On Wed, 3 Mar 2004, Ruby Baby wrote: > I try to avoid questions like "Why doesn't Ruby do what ___ does?" > > But I'm stumped by hashes and asking for help to find a new solution. > > I use MySQL databases a lot. Let's say I have a query to find the > top-selling albums in my store: > > SELECT sku, artistname, albumtitle, sold FROM albums > ORDER BY sold DESC LIMIT 20; > > When I get the hash of results from the database, I'm used to making > the "sku" the hash key since it's the only unique identifier. > A perfect hash key. Used everywhere by my iteration methods. > > In PHP, this is no problem. I get the hash, and I can do the > "each" iteration on it, and get my top-selling albums in order. > > In Ruby, it's an unordered jumble. > > Why is that? > > And what do you think is the Ruby way solution for what I need to do? http://raa.ruby-lang.org/list.rhtml?name=ruby-rbtree it is ultra-fast and the keys come out in order: ~ > cat a.rb #!/usr/bin/env ruby require 'rbtree' rb = RBTree.new 42.times{|i| rb[i] = i} rb.each{|k,v| puts "#{ k } => #{ v }"} ~ > a.rb 0 => 0 1 => 1 2 => 2 3 => 3 39 => 39 40 => 40 41 => 41 -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | URL :: http://www.ngdc.noaa.gov/stp/ | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done ===============================================================================