Welcome, guest | Sign In | My Account | Store | Cart

This function allows you to easily sort a list by multiple columns in ascending and descending orders similar in function to the ORDER BY clause in SQL.

Python, 9 lines
def orderBy(sortlist, orderby=[], desc=[]):
    '''orderBy(sortlist, orderby, desc) >> List
    sortlist: list to be sorted
    orderby: list of field indexes
    desc: list of field indexes that are to be sorted descending'''      
    for i in orderby:
        sortlist.sort(lambda x, y: cmp(*[(x[i], y[i]), (y[i], x[i])][i in desc]))
    return sortlist

Imagine you have a list:

New = [[1, 50.00, 'Bob'], [4, 50.00, 'Mary'], [3, 500.00, 'Tom'], [5, 75.00, 'Tracy'], [7, 175.00, 'Linda'], [2, 75.00, 'Charles']]

Now imagine this list is a SQL table (with fields called Loans, Amount, Name) and you want them in a particular order You might say:

  FROM New
 ORDER BY Amount DESC, Loans, Name

We are asking for the Amount field to be sorted in descending order.

To duplicate this using the above function:

NewList = orderBy(New, [1, 0, 2], [1])


Jonathan Wright 19 years, 7 months ago  # | flag

and in Python 2.4.

def orderBy(sortlist, orderby=[], desc=[]):
    for i in reversed(orderby):
        sortlist.sort(key=operator.itemgetter(i), reverse=(i in desc))
    return sortlist
pythondev dev 19 years, 6 months ago  # | flag

please explain. can you please explain the code which Steve Lucy has written. what is the use of * how is the cmp function working inhis code thanks

Steve Lucy (author) 19 years, 5 months ago  # | flag

The * causes the tuple we choose, (x[i], y[i]) or (y[i], x[i]), to be mapped to the function's arguments, instead of being passed as a single argument (i.e., cmp(x[i], y[i]) and not cmp((x[i], y[i]))

Let's say you have a function:

def Add(a, b):
    return a + b

and a list of values you want added:

w = [(1, 2),
     (2, 2),
     (3, 4)]

instead of saying something like:

for a, b in w:
    print Add(a, b)

You could say:

for i in w:
    print Add(*i)
Created by Steve Lucy on Wed, 22 Sep 2004 (PSF)
Python recipes (4591)
Steve Lucy's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks