Logo

Programming-Idioms

History of Idiom 28 > diff from v56 to v57

Edit summary for version 57 by tomdml:
[Python] Added closing parentheses.

Version 56

2020-04-29, 09:12:34

Version 57

2020-04-29, 09:13:00

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 the 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 the type Item of the objects in items.

Imports
from operator import attrgetter
Imports
from operator import attrgetter
Code
items = sorted(items, key=attrgetter('p')
Code
items = sorted(items, key=attrgetter('p'))
Comments bubble
We use attrgetter to avoid having to write a lambda. The Operator module contains lots of useful functions that can be passed to higher-order functions.
Comments bubble
We use attrgetter to avoid having to write a lambda. The Operator module contains lots of useful functions that can be passed to higher-order functions.