Programming-Idioms

History of Idiom 28 > diff from v7 to v8

Edit summary for version 8 :

Version 7

2015-07-31, 19:31:25

Version 8

2015-08-01, 01:14:55

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of type Item of the objects in items.

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of type Item of the objects in items.

Code
items = sorted(items, key=lambda x: x.p)
Comments bubble
The lambda expression pulls out the field you want to sort by. If you want to sort in reverse order, add reverse=True to the argument list.