Programming-Idioms

History of Idiom 28 > diff from v6 to v7

Edit summary for version 7 :

Version 6

Version 7

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
@sorted = sort { $a->{property} cmp $b->{property} } @unsorted;
Comments bubble
The sort function is given references to two items in the list to sort $a and $b, so you simply write a predicate to sort the list.