Logo

Programming-Idioms

History of Idiom 28 > diff from v85 to v86

Edit summary for version 86 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 85

2022-06-21, 17:16:37

Version 86

2022-08-19, 12:59:03

Idiom #28 Sort by a property

Sort the elements of the list (or 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 the elements of the list (or 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
Imports
import "golang.org/x/exp/slices"
Code
less := func(a, b Item) bool {
	return a.p < b.p
}
slices.SortFunc(items, less)
Comments bubble
SortFunc is generic and type-safe at compile time.
Doc URL
https://pkg.go.dev/golang.org/x/exp/slices#SortFunc
Demo URL
https://go.dev/play/p/g8t9v_ahMoo