Hello, the code I am posting sorts a pivot table but there can be only one statistic per row e.g Mean or Count.

I often have multiple statistics per row e.g Mean, Mode etc... .

I often have to generate multiple, long, pivot tables and to sort them manually is tedious made all

I the more so by having code that almost dose this for me.

All the pivot tables I make have the Mean as a row statistic, can some one see how to alter the following so I could sort on the Mean if there are multiple row statistics.

I am new to python, if this was VBA I could make a go at it.

I know it is not the best form to post a question were you ask: Please do this for me, but that is my skill level in python at the moment.

Thank for any assistance.

def sortTable(obj, i, j, numrows, numcols, section, more, custom):
    """Sort the rows of the table according to the selected column values
    Cell formats are NOT updated, so the formats for all cells in a column
    should be the same.
    custom parameters is direction ('a', the default, or 'd')"""
    if not section == "datacells":
    direction = custom.get("direction", "a")
    if not direction in ['a', 'd']:
        print "direction must be 'a' or 'd'"
        raise ValueError
    PvtMgr = more.thetable.PivotManager()
    numrowdims = PvtMgr.GetNumRowDimensions()
    if numrowdims != 1:
        print "Cannot sort table unless there is exactly one row dimension"
        raise ValueError

    col = j   # sorting column

I can not read your code, but does this code help you.

I define average for rows and printing function with less desimals in order to print matrix more clearly.

I make random matrix x, take copy of it named y, add averages to end of rows of y and finally make z from y sorted by row averages at the end of rows.

def average(x):
    for i in range(len(x)):

def matprint(x):
    for i in x:
        for j in i:
            print "%.5f" % j,


x=[[random.random() for y in range(n)] for x in range(n)]
print '-'*20,'matrix x','-'*20


print '-'*20,'matrix y, with average in the end of row','-'*20

z=sorted(y,key=lambda y:y[-1])

print '-'*20,'matrix z, sorted by average','-'*20