SortBy
Overview
SortBy returns a sorted copy ordered by fn's key (lodash sortBy).
If you are coming from Node.js, the closest pattern is _.sortBy(arr, fn).
Signature
go
func SortBy[T any, K cmp.Ordered](in []T, fn func(T) K) []TCompare: Node.js · Standard Go · gox
js
_.sortBy(arr, fn)go
// Manual loop or Go 1.21+ slices package
for _, v := range items {
// transform or filter v
}go
import "github.com/sahilkhaire/gox/slice"
slice.SortBy(arr, fn)Example
go
import "github.com/sahilkhaire/gox/slice"
slice.SortBy(arr, fn)Tips
Chain Filter, Map, and Reduce for lodash-style pipelines. Results are new slices — inputs are never mutated.
Standard library alternative
Use a for loop or Go 1.21+ slices package helpers from the standard library.