# Sorting Using Python Sorted Function

**URL:** <https://forum.inductiveautomation.com/t/sorting-using-python-sorted-function/74181>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [May 25, 2023, 3:59pm UTC](https://forum.inductiveautomation.com/t/sorting-using-python-sorted-function/74181 "2023-05-25T15:59:08Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 25, 2023, 4:21pm UTC](https://forum.inductiveautomation.com/t/sorting-using-python-sorted-function/74181/2 "2023-05-25T16:21:50Z")

</div>

`sorted` accepts a keyword argument `key`:  
[https://docs.python.org/2.7/library/functions.html#sorted](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:

```python
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.

---

_[View the full topic](https://forum.inductiveautomation.com/t/sorting-using-python-sorted-function/74181)._
