Documentation ¶
Index ¶
- type CompareOption
- type Comparison
- func BoolsComparator(_ ...CompareOption) Comparison[bool]
- func BoolsPtrComparator(opts ...CompareOption) Comparison[*bool]
- func BytesComparator(opts ...CompareOption) Comparison[[]byte]
- func CompoundCriteria[T any](criteria ...Comparison[T]) Comparison[T]
- func OrderedComparator[T Ordered]() Comparison[T]
- func OrderedPtrComparator[T Ordered]() Comparison[*T]
- func Ptr[T any](in Comparison[T]) Comparison[*T]
- func Reverse[T any](in Comparison[T]) Comparison[T]
- func ReversePtr[T any](in Comparison[*T]) Comparison[*T]
- func StringsComparator(opts ...CompareOption) Comparison[string]
- func StringsPtrComparator(opts ...CompareOption) Comparison[*string]
- type Multi
- type Ordered
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompareOption ¶
type CompareOption func(*compareOptions)
CompareOption sets options for sorting.
func WithLocale ¶
func WithLocale(locale string) CompareOption
WithLocale sets the language specific collation for ordering strings.
func WithLocaleTag ¶
func WithLocaleTag(localeTag language.Tag) CompareOption
WithLocaleTag sets the language specific collation for ordering strings.
type Comparison ¶
func BoolsComparator ¶
func BoolsComparator(_ ...CompareOption) Comparison[bool]
BoolsComparator compare booleans: false < true.
func BoolsPtrComparator ¶
func BoolsPtrComparator(opts ...CompareOption) Comparison[*bool]
BoolsPtrComparator compare pointers to booleans: false < true.
Pointer logic is that nil < !nil , nil == nil. Usual comparison occurs whenever both arguments are not nil.
func BytesComparator ¶
func BytesComparator(opts ...CompareOption) Comparison[[]byte]
BytesComparator builds a comparator for []byte slices, with some collating sequence option.
func CompoundCriteria ¶
func CompoundCriteria[T any](criteria ...Comparison[T]) Comparison[T]
CompoundCriteria compose multiple sorting criteria into a single Comparison[T].
func OrderedComparator ¶
func OrderedComparator[T Ordered]() Comparison[T]
OrderedComparator builds a comparator for any ordered golang type (i.e. numerical types).
func OrderedPtrComparator ¶
func OrderedPtrComparator[T Ordered]() Comparison[*T]
OrderedPtrComparator builds a comparator for pointers to any ordered golang type (i.e. numerical types).
Pointer logic is that nil < !nil , nil == nil. Usual comparison occurs whenever both arguments are not nil.
func Ptr ¶
func Ptr[T any](in Comparison[T]) Comparison[*T]
Ptr makes a comparison over pointers from a comparison over types.
func ReversePtr ¶
func ReversePtr[T any](in Comparison[*T]) Comparison[*T]
ReversePtr reverses the nil comparison logic for pointer operands: !nil < nil instead of nil < !nil.
The comparison logic for non-nil values is not altered.
func StringsComparator ¶
func StringsComparator(opts ...CompareOption) Comparison[string]
StringsComparator builds a comparator for strings, with some collating sequence option.
func StringsPtrComparator ¶
func StringsPtrComparator(opts ...CompareOption) Comparison[*string]
StringsPtrComparator builds a comparator for pointers to strings, with some collating sequence option.
Pointer logic is that nil < !nil , nil == nil. Usual comparison occurs whenever both arguments are not nil.
type Multi ¶
type Multi[T any] struct { // contains filtered or unexported fields }
func NewMulti ¶
func NewMulti[T any](collection []T, criteria ...Comparison[T]) *Multi[T]
NewMulti produces a sortable object, that supports multiple sorting criteria.
func (Multi[T]) Collection ¶
func (s Multi[T]) Collection() []T
Collection yields the inner collection.
func (*Multi[T]) Sort ¶
func (s *Multi[T]) Sort()
Sort the inner collection. This is a shorthand for sort.Sort(s).
Example ¶
package main import ( "encoding/json" "fmt" "github.com/fredbi/go-patterns/sorters" "github.com/go-openapi/swag" ) type SampleUser struct { ID string `json:"id"` FirstName *string `json:"first_name,omitempty"` LastName string `json:"last_name"` Age *float64 `json:"age,omitempty"` } func main() { // builds a sortable collection with multiple sorting criteria. s := sorters.NewMulti[SampleUser]( sampleUsers(), // order by: // * LastName ASC // * FirstName ASC // * Age ASC // * ID DESC func(a, b SampleUser) int { // Alternatively, we may use WithLocaleTag(language.French) to specify the collating sequence. return sorters.StringsComparator(sorters.WithLocale("fr"))(a.LastName, b.LastName) }, func(a, b SampleUser) int { // Pointer logic is that nil < !nil , nil == nil. Usual comparison occurs whenever // both arguments are not nil return sorters.StringsPtrComparator(sorters.WithLocale("fr"))(a.FirstName, b.FirstName) }, func(a, b SampleUser) int { // Ordered are go numerical types return sorters.OrderedPtrComparator[float64]()(a.Age, b.Age) }, func(a, b SampleUser) int { return sorters.Reverse( // The default string comparison operates here (no language collation) sorters.StringsComparator(), )(a.ID, b.ID) }, ) // alternatively, we can call the sort package: sort.Sort(s) s.Sort() jazon, err := json.MarshalIndent(s.Collection(), "", " ") if err != nil { fmt.Printf("error: %v\n", err) } fmt.Println(string(jazon)) } func sampleUsers() []SampleUser { return []SampleUser{ { ID: "1", FirstName: swag.String("Fred"), LastName: "B.", Age: swag.Float64(49), }, { ID: "4", FirstName: swag.String("Fred"), LastName: "B.", Age: swag.Float64(49), }, { ID: "10", FirstName: swag.String("Fred"), LastName: "B.", Age: swag.Float64(25), }, { ID: "5", LastName: "L.", Age: swag.Float64(45), }, { ID: "0", FirstName: swag.String("Enzo"), LastName: "L.", }, { ID: "2", FirstName: swag.String("Fred"), LastName: "B.", Age: swag.Float64(49), }, { ID: "5", FirstName: swag.String("Thomas"), LastName: "B.", Age: swag.Float64(13), }, { ID: "2", FirstName: swag.String("Mathieu"), LastName: "B.", Age: swag.Float64(15), }, } }
Output: [ { "id": "10", "first_name": "Fred", "last_name": "B.", "age": 25 }, { "id": "4", "first_name": "Fred", "last_name": "B.", "age": 49 }, { "id": "2", "first_name": "Fred", "last_name": "B.", "age": 49 }, { "id": "1", "first_name": "Fred", "last_name": "B.", "age": 49 }, { "id": "2", "first_name": "Mathieu", "last_name": "B.", "age": 15 }, { "id": "5", "first_name": "Thomas", "last_name": "B.", "age": 13 }, { "id": "5", "last_name": "L.", "age": 45 }, { "id": "0", "first_name": "Enzo", "last_name": "L." } ]
type Ordered ¶
type Ordered = constraints.Ordered
Ordered defines all ordered types (see https://go.dev/ref/spec)