From: Ruby Baby Date: 2004-03-04T06:01:45+09:00 Subject: Re: why are Hashes so unsorted? what's your solution? Thanks for your help and advice, everyone. The description, below, is correct. Since I learned PHP first, I started heavily relying on this, not knowing it was a PHP quirk. So when I learned Ruby, I assumed "Hash" was the same as PHP's "associative array" and was surprised they weren't in order. > PHP blurs the lines between hashes, > arrays, whatever you want to call them. There is just one construct, > which they call an Array. Each item in the array can have both a hash > key (a string) and an index (an integer). if you write "myarray['test'] > = item", an index automatically gets created in addition to the hash > key. > Additionally, PHP arrays have an order, which is not necessarily the > same as the order given by the indices. In fact, it is *very* often > different. Usually it is the order in which items were added to the > array. Calling "each()" iterates over the array in the hidden order. If > you want, you can sort the array/hash by the internal/hidden ordering, > and indices and hashes are unchanged, but iterating over it with > "each()" is different. > > Of course, this sounds confusing all thrown together like this, but it > really isn't that bad. You can do some nifty tricks, all because of the > strange hybrid php hash/lists. :)