A previous post provided a VBA Sort function that could be used as a User Defined Function (UDF) to provide a sorted list on the spreadsheet, or called from another VBA routine to sort an array.
I have just written a Python/PyXll routine to provide the same functionality, including sorting on any specified column, and sorting in ascending or descending order. Here is the code in full:
from operator import itemgetter @xl_func("var srange, int sortcol, bool rev : var") def py_Sort(srange, sortcol, rev): return sorted(srange, key = itemgetter(sortcol), reverse = rev)
The py_Sort function requires Python and PyXll to be installed, but otherwise works the same as the VBA function, and a quick test indicates that it is about twice as fast. The screen-shot below shows output from the VBA and Python function: