RE: Combined natural and unnatural list sorting

From: Delaney, Timothy C (Timothy) (tdelaney_at_avaya.com)
Date: 06/15/04


Date: Wed, 16 Jun 2004 07:51:42 +1000
To: <python-list@python.org>

Derek Basch wrote:

> 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.
>
> I looked for a while at using comparison functions with sort but I
> don't think that will work. Anyone been down this road? Suggestions?

Of course a comparison function will work - you just have to write it
correctly.

I'll using DSU (decorate-sort-undecorate) for this example. Every
comparison function can be converted to DSU and vice versa.

In the comparison function, you should do the following:

1. Split the string into two parts (i.e. split on '/').

2. Do a string compare on the first part (colour).

3. If the first part compares equal, look up an equivalent value for
each size and compare those. I would suggest building a dictionary
mapping the (string) size to an integer e.g.

SIZE_MAP = {
    'S': 1,
    'M': 2,
    'L': 3,
    'XL': 4,
}

Tim Delaney



Relevant Pages

  • Re: sorting dates
    ... >The string method runs about 60 times faster, so if speed does matter, ... Your date array appears to hold entries such as 13-Apr-05. ... Your string sort first converts those to 2005-04-13 (the hyphens are a ... Object array, and use a comparison function that subtracts the Objects, ...
    (comp.lang.javascript)
  • Re: When random isnt random
    ... reliable shuffling is not a special case of sorting. ... Personally I think it would be more important to add Sort and Move methods ... > data is not necessarily the one provided for sorting quasi-random data. ... So I don't recommend to use a random comparison function any more, ...
    (borland.public.delphi.language.objectpascal)
  • Re: TList.Sort running 3 times?
    ... The next loop will call the comparison function twice. ... SCompare will return 1 the first time. ... positions at the outset rather than calling Sort each time you call Add. ...
    (alt.comp.lang.borland-delphi)
  • Re: Python 3.0, rich comparisons and sorting order
    ... I'm changing my approach to the sorting ... sort() already accepts a key function to be passed. ... passing a comparison function to sort; ... blog: http://rascunhosrotos.blogspot.com blog: http://pythonnotes.blogspot.com mail: carribeiro@gmail.com ...
    (comp.lang.python)
  • Re: sorting list of tuples by second (third...) tuple item
    ... I stepped into this an old post: (Thu Feb 14 20:40:08 CET ... element that you want to sort on, and the entire line, sorts that ... When compared, tuples first compare on the first element, then on the second etc... ... In these cases I had to use an ad hoc comparison function. ...
    (comp.lang.python)