[Tutor] Sort pointers -

Liam Clarke cyresse at gmail.com
Wed Nov 24 09:51:14 CET 2004


Kia ora,

Just exploring the wonderful world of dictionaries, and I'm trying to
comprehend the sort() method, because as I found, dictionaries are not
sorted.

So, I've got a dictionary, miscPeople= {'James Bond' : '007' , 'Enid
Blyton' : '005' , 'Enid Blyton also' : '006' , 'Captain Planet' :
'000' }

(Okay, so that's a sample dictionary)

So, if I wanted to sort by alphabetical key i.e. (first names here)

for name, numerical in miscPeopledict.sorted() (pseudo code):
   print name, numerical

Captain Planet 000
Enid Blyton 005
Enid Blyton also 006
James Bond 007

or conversely, to sort by value in some manner

for name, numerical in miscPeopledict.sorted.reverse() 

James Bond 007
Enid Blyton also 006
Enid Blyton 005
Captain Planet 000

That's just an example of output. I'm not too hung up on the output at
the moment, just sort().

It says to use a comparison function...

And I have no idea how to work that.

??? Very confused. I've tried searching for info, there's some very
complex ways out there to sort data, usually with proper first names
(Schwartzian transformations...), but I can't seem to find the simple.

Could anyone post some useful links for my education?

Much appreciated.

Liam Clarke


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list