From: Brian Candler Date: 2004-09-19T01:23:52+09:00 Subject: Re: Accessing Hash elements in sorted order? On Sat, Sep 18, 2004 at 04:58:13AM +0900, Markus wrote: > > > There has been some discussion going on whether Ruby's Hash should be > > > sorted by default on this list recently. > > > > > > > I would vote in favor of it. > > I'd vote against, for the following reasons. > > 1. Every time it comes up the thread goes quite a while before the > advocates realize that they aren't all assuming the same sorting > order. Some are thinking "sorted by key (obviously)" while > others are thinking "sorted by order of insertion (obviously)" > and still others are thinking "sorted by value" or "sorted by > some arbitrary key so long as it's consistent," etc. > 2. Many of the proposals are not particularly well defined when you > consider issues like modification and alternate means of > construction. > 3. Hash, like String, Array, etc. is a fairly well established data > structure. Although it might for some uses be nice to have a > string where the characters were "automatically" sorted into > alphabetical order, this isn't what anyone familiar with strings > would expect. > 4. It may well impose a significant performance penalty > 5. It may well break existing code > 6. It is easy enough to produce the desired effect(s) by other > means. 7. It would almost certainly no longer be a Hash!! In other words, to get any efficiency for the 'each' method, you would have to implement it as a B-Tree or similar data structure, not a hash. I'm sure there's a tree library in RAA, so you can always just use that instead. That's unless 'sorted' simply means 'kept in order of insertion'. In that case, it could be a combination of a hash and a doubly-linked list. But that means it still wouldn't be just a hash. Regards, Brian.