'Sorting with Sorted function and lambda
Found this code snippet which is sorting a list of items by display_order:
conditions = sorted(conditions,
key=lambda x: (x.display_order, x.condition_guid != condition.condition_guid))
Where conditions is a list:
conditions: [
Conditions 49, 015c0dec-26b8-4343-93fc-27d36548ae0a,
Conditions 48, 0bab9d83-b730-4c23-986b-e3ff5b0240a2,
Conditions 50, 7cd47bd1-0073-45a4-9793-3ed83f93b213,
Conditions 51, ce684f3c-954e-43af-8289-77c4cd835131,
Conditions 52, 8fce7db6-6813-4a46-a56c-731b7eb2d4bf]
Sorted function follows this syntax:
sorted(iterable, key=None, reverse=False)
where iterable = conditions.
Wondering the meaning of the parenthesis and comma in the lambda function:
(x.display_order, x.condition_guid != condition.condition_guid)
Solution 1:[1]
key in sorted does not have to be a single value, it can be a set to provide secondary sorting characteristics.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eumel |
