sort

package
v1.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2026 License: BSD-2-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Func

func Func() func(a, b reflect.Value) int

Func returns a function able to compare 2 reflect.Value values.

The sorting rules are listed in Values documentation.

Cyclic references are correctly handled.

func MapEach

func MapEach(m reflect.Value, fn func(k, v reflect.Value) bool) bool

MapEach calls fn for each key/value pair of map m using the order of keys. If fn returns false, it will not be called again. MapEach returns false if fn returned false.

func Values

func Values(s []reflect.Value) sort.Interface

Values is used to allow the sorting of a []reflect.Value slice. It is used with the standard sort package:

vals := []reflect.Value{a, b, c, d}
sort.Sort(Values(vals))
// vals contents now sorted

Replace sort.Sort by sort.Stable for a stable sort. See sort documentation.

Sorting rules are as follows:

  • invalid value is always lower
  • nil is always lower
  • different types are sorted by their name
  • if method TYPE.Compare(TYPE) int exits, calls it
  • false is lesser than true
  • float and int numbers are sorted by their value, NaN is always lower
  • complex numbers are sorted by their real, then by their imaginary parts
  • strings are sorted by their value
  • map: shorter length is lesser, then sorted by address
  • functions, channels and unsafe pointer are sorted by their address
  • struct: comparison is spread to each field
  • pointer: comparison is spread to the pointed value
  • arrays: comparison is spread to each item
  • slice: comparison is spread to each item, then shorter length is lesser
  • interface: comparison is spread to the value

Cyclic references are correctly handled.

See also Func.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL