Skip to content
Node: _.sortBy(arr, fn)github.com/sahilkhaire/gox/slice

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) []T

Compare: 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.

Back to slice package overview

MIT Licensed · Built for Node.js developers moving to Go