go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/funcs/sort.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package funcs 9 10 import ( 11 "cmp" 12 "context" 13 "slices" 14 "time" 15 16 "go.charczuk.com/projects/nodes/pkg/incrutil" 17 "go.charczuk.com/sdk/iter" 18 ) 19 20 func Sort[T cmp.Ordered](ctx context.Context, inputValues *incrutil.Inputs[T]) ([]T, error) { 21 inputs := inputValues.Values() 22 slices.Sort(inputs) 23 return inputs, nil 24 } 25 26 func SortMany[T cmp.Ordered](ctx context.Context, inputValues *incrutil.Inputs[[]T]) ([]T, error) { 27 inputs := inputValues.Values() 28 merged := iter.MergeMany(inputs) 29 slices.Sort(merged) 30 return merged, nil 31 } 32 33 func TimestampAsc(t0, t1 time.Time) int { 34 if t0.Equal(t1) { 35 return 0 36 } 37 if t0.Before(t1) { 38 return -1 39 } 40 return 1 41 } 42 43 func SortComparer[T any](comparers ...iter.SorterComparer[T]) func(context.Context, *incrutil.Inputs[T]) ([]T, error) { 44 return func(_ context.Context, inputValues *incrutil.Inputs[T]) ([]T, error) { 45 inputs := inputValues.Values() 46 iter.Sort(inputs, comparers...) 47 return inputs, nil 48 } 49 } 50 51 func SortComparerMany[T any](comparers ...iter.SorterComparer[T]) func(context.Context, *incrutil.Inputs[[]T]) ([]T, error) { 52 return func(_ context.Context, inputValues *incrutil.Inputs[[]T]) ([]T, error) { 53 inputs := inputValues.Values() 54 merged := iter.MergeMany(inputs) 55 iter.Sort(merged, comparers...) 56 return merged, nil 57 } 58 }