History of Idiom 28 > diff from v9 to v10
Edit summary for version 10 :
↷
Version 9
2015-08-01, 01:16:29
Version 10
2015-08-01, 01:19:18
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 = sort { $a->{p} cmp $b->{p} } @items;
Code
@items = sort { $a->{p} cmp $b->{p} } @items;
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. The cmp operator sorts lexically (i.e., like strings), while the <=> operator sorts numerically. Swap the positions of $a and $b to sort in the reverse direction.
Comments bubble
sort gives your code block two items to sort: $a and $b. Two operators are commonly used: cmp to sort lexically, and <=> to sort numerically. Swapping the positions of $a and $b reverses the sort direction.