Documentation ¶
Overview ¶
Sortutil is a Go library which lets you sort a slice without implementing a sort.Interface, and in different orderings: ascending, descending, or case-insensitive ascending or descending (for slices of strings.)
Additionally, Sortutil lets you sort a slice of a custom struct by a given struct field or index--for example, you can sort a []MyStruct by the structs' "Name" fields, or a [][]int by the second index of each nested slice, similar to using sorted(key=operator.itemgetter/attrgetter) in Python.
Index ¶
- func Asc(slice interface{})
- func AscByField(slice interface{}, name string)
- func AscByFieldIndex(slice interface{}, index []int)
- func AscByIndex(slice interface{}, index int)
- func CiAsc(slice interface{})
- func CiAscByField(slice interface{}, name string)
- func CiAscByFieldIndex(slice interface{}, index []int)
- func CiAscByIndex(slice interface{}, index int)
- func CiDesc(slice interface{})
- func CiDescByField(slice interface{}, name string)
- func CiDescByFieldIndex(slice interface{}, index []int)
- func CiDescByIndex(slice interface{}, index int)
- func Desc(slice interface{})
- func DescByField(slice interface{}, name string)
- func DescByFieldIndex(slice interface{}, index []int)
- func DescByIndex(slice interface{}, index int)
- func Reverse(slice interface{})
- func ReverseInterface(s sort.Interface)
- func Sort(slice interface{}, getter Getter, ordering Ordering)
- func SortReverseInterface(s sort.Interface)
- type Getter
- type Ordering
- type Sorter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AscByField ¶
func AscByField(slice interface{}, name string)
Sort a slice in ascending order by a field name.
func AscByFieldIndex ¶
func AscByFieldIndex(slice interface{}, index []int)
Sort a slice in ascending order by a list of nested field indices, e.g. 1, 2, 3 to sort by the third field from the struct in the second field of the struct in the first field of each struct in the slice.
func AscByIndex ¶
func AscByIndex(slice interface{}, index int)
Sort a slice in ascending order by an index in a child slice.
func CiAscByField ¶
func CiAscByField(slice interface{}, name string)
Sort a slice in case-insensitive ascending order by a field name. (Valid for string types.)
func CiAscByFieldIndex ¶
func CiAscByFieldIndex(slice interface{}, index []int)
Sort a slice in case-insensitive ascending order by a list of nested field indices, e.g. 1, 2, 3 to sort by the third field from the struct in the second field of the struct in the first field of each struct in the slice. (Valid for string types.)
func CiAscByIndex ¶
func CiAscByIndex(slice interface{}, index int)
Sort a slice in case-insensitive ascending order by an index in a child slice. (Valid for string types.)
func CiDescByField ¶
func CiDescByField(slice interface{}, name string)
Sort a slice in case-insensitive descending order by a field name. (Valid for string types.)
func CiDescByFieldIndex ¶
func CiDescByFieldIndex(slice interface{}, index []int)
Sort a slice in case-insensitive descending order by a list of nested field indices, e.g. 1, 2, 3 to sort by the third field from the struct in the second field of the struct in the first field of each struct in the slice. (Valid for string types.)
func CiDescByIndex ¶
func CiDescByIndex(slice interface{}, index int)
Sort a slice in case-insensitive descending order by an index in a child slice. (Valid for string types.)
func DescByField ¶
func DescByField(slice interface{}, name string)
Sort a slice in descending order by a field name.
func DescByFieldIndex ¶
func DescByFieldIndex(slice interface{}, index []int)
Sort a slice in descending order by a list of nested field indices, e.g. 1, 2, 3 to sort by the third field from the struct in the second field of the struct in the first field of each struct in the slice.
func DescByIndex ¶
func DescByIndex(slice interface{}, index int)
Sort a slice in descending order by an index in a child slice.
func ReverseInterface ¶
Reverse a type which implements sort.Interface.
func Sort ¶
Sort a slice using a Getter in the order specified by Ordering. getter may be nil if sorting a slice of a basic type where identifying a parent struct field or slice index isn't necessary, e.g. if sorting an []int, []string or []time.Time. A runtime panic will occur if getter is not applicable to the given data slice, or if the values retrieved by g cannot be compared.
func SortReverseInterface ¶
Sort a type using its existing sort.Interface, then reverse it. For a slice with a a "normal" sort interface (where Less returns true if i is less than j), this causes the slice to be sorted in descending order.
Types ¶
type Getter ¶
A Getter is a function which takes a reflect.Value for a slice, and returns a a slice of reflect.Value, e.g. a slice with a reflect.Value for each of the Name fields from a reflect.Value for a slice of a struct type. It is used by the sort functions to identify the elements to sort by.
func FieldByIndexGetter ¶
Returns a Getter which gets nested fields corresponding to e.g. []int{1, 2, 3} = field 3 of field 2 of field 1 of each struct from a reflect.Value for a slice of a struct type, returning them as a slice of reflect.Value (one Value for each of the indices in the structs.) Can be used with Sort to sort an []Object by the first field in the struct value of the first field of each Object. A runtime panic will occur if the specified field isn't exported.
func FieldGetter ¶
Returns a Getter which gets fields with name from a reflect.Value for a slice of a struct type, returning them as a slice of reflect.Value (one Value for each field in each struct.) Can be used with Sort to sort an []Object by e.g. Object.Name or Object.Date. A runtime panic will occur if the specified field isn't exported.
func IndexGetter ¶
Returns a Getter which gets values with index from a reflect.Value for a slice. Can be used with Sort to sort an [][]int by e.g. the second element in each nested slice.
func SimpleGetter ¶
func SimpleGetter() Getter
Returns a Getter which returns the values from a reflect.Value for a slice. This is the default Getter used if none is passed to Sort.
type Ordering ¶
type Ordering int
Ordering decides the order in which the specified data is sorted.
A runtime panic will occur if case-insensitive is used when not sorting by a string type.
type Sorter ¶
type Sorter struct { Slice reflect.Value Getter Getter Ordering Ordering // contains filtered or unexported fields }
A reflecting sort.Interface adapter.
func New ¶
Returns a Sorter for a slice which will sort according to the items retrieved by getter, in the given ordering.