Logo

Programming-Idioms

History of Idiom 28 > diff from v6 to v7

Edit summary for version 7 by :

Version 6

2015-05-06, 21:04:49

Version 7

2015-07-31, 19:31:25

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.