Is there an easy way to sort a list by two criteria?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • neocortex

    Is there an easy way to sort a list by two criteria?

    Hello!
    I am a newbie in Python. Recently, I get stuck with the problem of
    sorting by two criteria. In brief, I have a two-dimensional list (for
    a table or a matrix). Now, I need to sort by two columns, but I cannot
    figure out how to do that. I read somewhere that it is possible to do:
    >>table.sort(). sort()
    but it does not work.
    Can anyone help me with this?
    PS: I am using Python under Ubuntu 6.06.

    Best,
    PM
  • Jeff Schwab

    #2
    Re: Is there an easy way to sort a list by two criteria?

    neocortex wrote:
    Hello!
    I am a newbie in Python. Recently, I get stuck with the problem of
    sorting by two criteria. In brief, I have a two-dimensional list (for
    a table or a matrix). Now, I need to sort by two columns, but I cannot
    figure out how to do that. I read somewhere that it is possible to do:
    >>>table.sort() .sort()
    but it does not work.
    Can anyone help me with this?
    PS: I am using Python under Ubuntu 6.06.
    You can specify an arbitrary comparison function with the cmp key to
    sort. IOW, use table.sort(cmp= f), where f is defined to compare table
    entries (rows?) by whichever criteria are required.

    Comment

    • Steven D'Aprano

      #3
      Re: Is there an easy way to sort a list by two criteria?

      On Sat, 09 Feb 2008 18:05:14 -0800, neocortex wrote:
      Hello!
      I am a newbie in Python. Recently, I get stuck with the problem of
      sorting by two criteria. In brief, I have a two-dimensional list (for a
      table or a matrix). Now, I need to sort by two columns, but I cannot
      figure out how to do that.

      Can you give a (small, simple) example of your data, and what you expect
      if you sort it successfully?

      I read somewhere that it is possible to do:
      >table.sort().s ort()
      but it does not work.
      No it doesn't, because the sort() method returns None, and None doesn't
      have a sort() method of its own.

      table.sort() will sort table in place, so you need something like this:
      >>table.sort( )
      >>table.sort( )
      except naturally that just does the exact same sort twice in a row, which
      is silly. So what you actually need is something like this:
      >>table.sort(ma gic goes here)
      >>table.sort(di fferent magic goes here)
      where the first piece of magic tells Python to sort by the first column,
      and the second by the second column. But to do that, we need to know more
      about how you're setting up the table.

      I'm *guessing* that you probably have something like this:


      table = [ ['fred', 35, 8], # name, age, score
      ['bill', 29, 8],
      ['betty', 30, 9],
      ['morris', 17, 4],
      ['catherine', 23, 6],
      ['anna', 45, 8],
      ['george', 19, 5],
      ['tanya', 27, 7],
      ]


      Now let's sort it:

      >>from operator import itemgetter
      >>import pprint
      >>table.sort(ke y=itemgetter(0) ) # sort by name
      >>table.sort(ke y=itemgetter(2) ) # sort by score
      >>pprint.pprint (table)
      [['morris', 17, 4],
      ['george', 19, 5],
      ['catherine', 23, 6],
      ['tanya', 27, 7],
      ['anna', 45, 8],
      ['bill', 29, 8],
      ['fred', 35, 8],
      ['betty', 30, 9]]



      Does this help?



      --
      Steven

      Comment

      • neocortex

        #4
        Re: Is there an easy way to sort a list by two criteria?

        Hello!
        Thank you all, so much! Now I can do double-criteria sort in at least
        three ways. More than I have expected.

        Best,
        PM

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: Is there an easy way to sort a list by two criteria?

          [repost]

          Duncan Booth:
          >from operator import itemgetter
          >lst = [(1,2,4),(3,2,1) ,(2,2,2),(2,1,4 ),(2,4,1)]
          >lst.sort(key=i temgetter(1))
          >lst.sort(key=i temgetter(2))
          >lst
          [(3, 2, 1), (2, 4, 1), (2, 2, 2), (2, 1, 4), (1, 2, 4)]
          A little known thing from Python 2.5:
          >>from operator import itemgetter
          >>lst = [(1,2,4),(3,2,1) ,(2,2,2),(2,1,4 ),(2,4,1)]
          >>sorted(lst, key=itemgetter( 2, 1))
          [(3, 2, 1), (2, 4, 1), (2, 2, 2), (2, 1, 4), (1, 2, 4)]

          Bye,
          bearophile

          Comment

          • thebjorn

            #6
            Re: Is there an easy way to sort a list by two criteria?

            On Feb 11, 10:47 am, bearophileH...@ lycos.com wrote:
            [...]
            A little known thing from Python 2.5:
            [...]
            >sorted(lst, key=itemgetter( 2, 1))
            Cute, thanks :-)

            --bjorn

            Comment

            Working...