Sorted iterator for mappings

Chris Angelico <[email protected]>
Newsgroups gmane.comp.lang.pike.user
Message-ID <CAPTjJmoE=0CHn=UA_gYcP4RwaDSXswgwVObWcTT=DhnfVyQHGQ@mail.gmail.com>
Often I want to iterate over a mapping, for some kind of display
purpose, and want to run through the elements in order. As far as I
know, there's no easy way to do that at the moment, so I usually end
up doing this:

foreach (sort(indices(some_map)), mixed key)

and then work with key and some_map[key]. In contrast, I can iterate
in arbitrary order thus:

foreach (some_map; mixed key; mixed value)

It'd be nice to have a way to use iterator syntax, but with a
guarantee that they're sorted. I've put this together as a
proof-of-concept:

https://github.com/Rosuav/shed/blob/master/sort_mapping.pike

Is there a better way to do this?

Could something like this possibly be added to the Mapping module?

ChrisA
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.