Programming-Idioms

History of Idiom 28 > diff from v62 to v63

Edit summary for version 63 :
New Groovy implementation by user [sbglasius]

Version 62

2020-10-13, 15:23:32

Version 63

2020-10-13, 15:24:50

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.

Variables
items,x,p
Variables
items,x,p
Code
items.sort { x, y -> x.p <=> y.p }
Comments bubble
Sort using the spaceship operator
Doc URL
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Iterable.html#sort(groovy.lang.Closure)