I often use my own mixin'd Hash#each_sorted that sorts the hash keys and then calls the block with the key and value. It would be useful to be added to Hash. (If this is already in Hash or Enumerable, I didn't see it.)
class Hash
def each_sorted(&block) # sort by key
self.keys.sort.each {|key| block.call(key, self[key])}
end
end
Agreed. If we can implement the feature without creating an
intermediate array, it might be useful. But I have no idea
to implement.
It would be possible, if you can tell if the current item is smaller
than the next item before fetching the latter. In other words, it'll
need a time machine.