Combined natural and unnatural list sorting

Terry Reedy tjreedy at udel.edu
Tue Jun 15 19:55:48 EDT 2004


"Derek Basch" <dbasch at yahoo.com> wrote in message
news:20040615213822.26735.qmail at web20810.mail.yahoo.com...
> Hello All,
>
> I need to sort a list using an unnatural sequence.
>
> I have a list like so:
>
> foo = ["White/M", "White/L", "White/XL", "White/S", "Black/S", "Black/M"]
>
> print foo.sort()
>
> ['White/L', 'White/M', 'White/S', 'White/XL', 'Black/M', 'Black/S']
>
>
> The order that I actually need is:
>
> ["White/S","White/M", "White/L", "White/XL", "Black/S", "Black/M"]
>
>
> So, in other words, I need the colors sorted alphabetically and the the
sizes
> sorted logically.

Last I knew, 'Black' sorts before, not after 'White' ;-)  Or do you really
want colors alphabetically?

TJR







More information about the Python-list mailing list