[2.5.1.1/dictionary] Change sorting order?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jan 22 14:32:24 CET 2010


On Fri, 22 Jan 2010 13:24:05 +0000, Steven D'Aprano wrote:

> On Fri, 22 Jan 2010 13:17:44 +0100, Gilles Ganault wrote:
[...]
>> I see that dictionaries can be sorted using the... sort() method, but
>> is it possible to have Python start sorting from a different letter?
> 
> You can write a customer sort routine by using the key parameter to
> sort, which will probably be messy. What I'd do is keep 27 lists of user
> names, according to the first letter (26 letters from A to Z, plus one
> extra):

Replying to myself... the first sign of insanity *wink*

While I'd still stick to that basic strategy, I'd wrap the functionality 
in a class so that the whole thing was transparent to the caller. So 
instead of the example I gave:

> # add a new user
> users[3].append('daniel')

I'd write the class so the caller just needed to do:

users.add('daniel')

and the class would calculate which inner list to append it to.

I just thought I'd make that clear in case you thought I expected you to 
manually keep track of which inner list each name should go into.


-- 
Steven



More information about the Python-list mailing list