Programming-Idioms

History of Idiom 28 > diff from v36 to v37

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

Version 36

2016-12-18, 02:55:34

Version 37

2017-02-23, 12:12:42

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.

Imports
import "sort"
Code
less := func(i, j int) bool {
	return items[i].p < items[j].p
}
sort.Slice(items, less)
Comments bubble
This is the diomatic way since Go 1.8.
Doc URL
https://golang.org/pkg/sort/#Slice
Demo URL
https://play.golang.org/p/jMZ1croPLE