Re: Combined natural and unnatural list sorting

Discussion in 'Python' started by Terry Reedy, Jun 16, 2004.

  1. Terry Reedy

    Terry Reedy Guest

    "Derek Basch" <> wrote in message
    news:...
    > 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
    Terry Reedy, Jun 16, 2004
    #1
    1. Advertising

  2. Terry Reedy

    Derek Basch Guest

    In article <>,
    says...
    >
    > "Derek Basch" <> wrote in message
    > news:...
    > > 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
    >
    >
    >
    >
    >


    Your right, B comes before W. I will go back to my dunce corner now :)

    Derek
    Derek Basch, Jun 16, 2004
    #2
    1. Advertising

  3. Terry Reedy

    Terry Reedy Guest

    "Derek Basch" <> wrote in message
    news:...
    > In article <>,
    > says...
    > >
    > > "Derek Basch" <> wrote in message
    > > news:...
    > > > 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']


    > > Last I knew, 'Black' sorts before, not after 'White' ;-)


    > Your right, B comes before W. I will go back to my dunce corner now :)


    Better to learn the lesson I and others have learned the hard way: either
    label posted code untested or test it and copy and paste both the code and
    the actual output.

    >>Or do you really want colors [non]alphabetically?


    Once you use a custom sort order for the sizes, adding a 'logical' color
    order like White, Yellow, Orange, Red, Green, Blue, Purple, Black would not
    be too much extra work.

    Terry J. Reedy
    Terry Reedy, Jun 16, 2004
    #3
  4. On Wed, 16 Jun 2004 00:23:47 GMT, Derek Basch <>
    wrote:

    >> > I need to sort a list using an unnatural sequence.


    ....

    This is my first attempt, I'm new to python so may be there are
    better ways to do it...

    >>> def scol(x,y):

    ... (cx,sx) = x.split("/")
    ... (cy,sy) = y.split("/")
    ... if sx==sy: return cmp(cx,cy)
    ... return cmp(xs[sx],xs[sy])
    ...
    >>> xs = dict(S=1,M=2,L=3)
    >>> a = ["White/M","White/L","White/S",

    "Orange/L","Blue/M","Purple/L"]
    >>> a.sort(scol)
    >>> a

    ['White/S', 'Blue/M', 'White/M', 'Orange/L', 'Purple/L', 'White/L']
    >>>


    I can think to a probably faster way if the list is big...
    just "transform" every string to a (xs[sx],cx) tuple once;
    then do a regular sort, then transform back to the string form.

    HTH
    Andrea
    Andrea Griffini, Jun 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sks

    Natural string sorting

    sks, Jun 18, 2004, in forum: Java
    Replies:
    6
    Views:
    491
    Roedy Green
    Jun 18, 2004
  2. Paul
    Replies:
    1
    Views:
    13,153
    Rogan Dawes
    Sep 14, 2004
  3. C. Barnes

    Code snippet: Natural string sorting

    C. Barnes, Jun 9, 2004, in forum: Python
    Replies:
    1
    Views:
    489
    Matteo Dell'Amico
    Jun 9, 2004
  4. Derek Basch
    Replies:
    3
    Views:
    303
    Derek Basch
    Jun 17, 2004
  5. Pekka Niiranen

    Unnatural behaviour in Pickling

    Pekka Niiranen, Jun 17, 2004, in forum: Python
    Replies:
    1
    Views:
    238
    Reinhold Birkenfeld
    Jun 17, 2004
Loading...

Share This Page