sorted
accepts a keyword argument key
:
https://docs.python.org/2.7/library/functions.html#sorted
key
should be a callable that returns a sortable item per value. Typically this is a simple lambda. Simplifying your script a little, and running it on some test data, you can return a sorted list with something like this:
returnedData = system.dataset.toPyDataSet(system.dataset.toDataSet(
["Clock Number", "Name", "Department", "Hours"],
[
["100003683", "Bob Vance", 9, 24.0],
["100008031", "Jim Hopper", 9, 32.6],
["100012345", "Amos Burton", 1, 40],
]
))
return sorted(
dict(zip(returnedData.columnNames, list(row)))
for row in returnedData,
key = lambda columns: columns['Hours']
)
The lambda is a function that will be called for each item in the comparison. It accepts the single argument in the list (in this case, the dictionary per row); the lambda is declaring this as being named columns
. Then columns['Hours']
is returning the value of the 'Hours' column, which will be used for the sorting operation. If you wanted to sort by multiple values, you would return a tuple with each value to sort by, in order, e.g.:
key = lambda columns: (columns['Department'], columns['Hours'])
Where the return value will first be sorted by department, then by hours.