Sorting a dict (was: How to sort using hash's key?)

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Jan 31 18:34:40 EST 2007


JoJo <Jstone at 3web.com> writes:

> I want to sort a dict via its key,but I have no idea on how to do
> it.

The 'dict' type is explicitly unordered. Sorting it isn't
possible. (Also note that a 'hash' is something entirely different in
Python.)

You can create your own type, inheriting from 'dict', that has the
behaviour you want. See the Python Cookbook for some examples.

    <URL:http://aspn.activestate.com/ASPN/Python/Cookbook/>

-- 
 \     "I cannot conceive that anybody will require multiplications at |
  `\   the rate of 40,000 or even 4,000 per hour ..."  -- F. H. Wales, |
_o__)                                                             1936 |
Ben Finney




More information about the Python-list mailing list