Documentation
¶
Index ¶
- func Sort[T any](values []T, compare Comparator[T])
- func Sorted[T any](values []T, compare Comparator[T]) []T
- func Stable[T any](values []T, compare Comparator[T])
- func StableSorted[T any](values []T, compare Comparator[T]) []T
- type Comparator
- func Asc[T any, V cmp.Ordered](selectValue func(T) V) Comparator[T]
- func By[T any, V any](selectValue func(T) V, compare func(a, b V) int) Comparator[T]
- func Chain[T any](comparators ...Comparator[T]) Comparator[T]
- func Desc[T any, V cmp.Ordered](selectValue func(T) V) Comparator[T]
- func NilFirst[T any](compareValue Comparator[T]) Comparator[*T]
- func NilLast[T any](compareValue Comparator[T]) Comparator[*T]
- func Reverse[T any](compare Comparator[T]) Comparator[T]
- type Registry
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Sorted ¶
func Sorted[T any](values []T, compare Comparator[T]) []T
Sorted returns an ordered copy of values.
func Stable ¶
func Stable[T any](values []T, compare Comparator[T])
Stable sorts values in place while preserving the order of equivalent values.
func StableSorted ¶
func StableSorted[T any](values []T, compare Comparator[T]) []T
StableSorted returns a stably ordered copy of values.
Types ¶
type Comparator ¶
Comparator compares two values using the slices.SortFunc contract.
func Asc ¶
func Asc[T any, V cmp.Ordered](selectValue func(T) V) Comparator[T]
Asc orders values by an ordered selected value in ascending order.
func By ¶
func By[T any, V any](selectValue func(T) V, compare func(a, b V) int) Comparator[T]
By builds a comparator from a selected value and a value comparator.
func Chain ¶
func Chain[T any](comparators ...Comparator[T]) Comparator[T]
Chain combines comparators in priority order.
Example ¶
package main
import (
"fmt"
"time"
"github.com/junghoon-vans/ord"
)
func main() {
type user struct {
id string
createdAt time.Time
}
users := []user{
{id: "u-3", createdAt: time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)},
{id: "u-2", createdAt: time.Date(2026, 1, 2, 9, 0, 0, 0, time.UTC)},
{id: "u-1", createdAt: time.Date(2026, 1, 2, 9, 0, 0, 0, time.UTC)},
}
compare := ord.Chain(
ord.By(func(u user) time.Time { return u.createdAt }, func(a, b time.Time) int {
return b.Compare(a)
}),
ord.Asc(func(u user) string { return u.id }),
)
for _, user := range ord.StableSorted(users, compare) {
fmt.Println(user.id)
}
}
Output: u-1 u-2 u-3
func Desc ¶
func Desc[T any, V cmp.Ordered](selectValue func(T) V) Comparator[T]
Desc orders values by an ordered selected value in descending order.
func NilFirst ¶
func NilFirst[T any](compareValue Comparator[T]) Comparator[*T]
NilFirst orders nil pointers before non-nil pointers. Non-nil values are delegated to compareValue.
func NilLast ¶
func NilLast[T any](compareValue Comparator[T]) Comparator[*T]
NilLast orders nil pointers after non-nil pointers. Non-nil values are delegated to compareValue.
Example ¶
package main
import (
"fmt"
"github.com/junghoon-vans/ord"
)
func main() {
type user struct {
id string
}
users := []*user{
{id: "u-2"},
nil,
{id: "u-1"},
}
compare := ord.NilLast(ord.Asc(func(u user) string { return u.id }))
for _, user := range ord.Sorted(users, compare) {
if user == nil {
fmt.Println("nil")
continue
}
fmt.Println(user.id)
}
}
Output: u-1 u-2 nil
func Reverse ¶
func Reverse[T any](compare Comparator[T]) Comparator[T]
Reverse flips a comparator's ordering.
type Registry ¶
type Registry[T any, K comparable] map[K]Comparator[T]
Registry stores typed, named order policies for one entity type.
Example ¶
package main
import (
"fmt"
"github.com/junghoon-vans/ord"
)
func main() {
type user struct {
id string
name string
}
type userOrder string
const userOrderName userOrder = "name"
orders := ord.Registry[user, userOrder]{
userOrderName: ord.Chain(
ord.Asc(func(u user) string { return u.name }),
ord.Asc(func(u user) string { return u.id }),
),
}
users := []user{
{id: "u-3", name: "Choi"},
{id: "u-2", name: "Ahn"},
{id: "u-1", name: "Ahn"},
}
compare, ok := orders.Get(userOrderName)
fmt.Println(ok)
for _, user := range ord.Sorted(users, compare) {
fmt.Println(user.id)
}
}
Output: true u-1 u-2 u-3
func (Registry[T, K]) Get ¶
func (r Registry[T, K]) Get(key K) (Comparator[T], bool)
Get returns a comparator by key.