sort

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package sort can be used to sort an array by their columns in either ascending or descending order.

Calling

sort.SortEntries(columnMap, entries, []string{"node", "-time"})

for example sorts the array by the time column in descending order and afterwards by the node column.

The "-" prefix means the sorter should use descending order. Sorting by multiple fields will be done from the last field to the first in a stable way - so the first column always gets the highest priority.

Three special cases exist:

  1. Non-existent columns will be silently ignored.
  2. When a virtual column is selected as a column to sort by, that column will be silently ignored.
  3. A column with a custom extractor is allowed to sort by. But the sorting function uses the underlying value instead of the result of the extractor function

One can use sort.CanSortBy(columnMap, []string{"node", "-time"}) to check if any column will be silently ignored. For more information the function sort.FilterSortableColumns(columnMap, []string{"node", "-time"}) can be used, which returns two lists. One with all valid filterable columns and another one with the invalid columns

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanSortBy added in v0.11.0

func CanSortBy[T any](cols columns.ColumnMap[T], sortBy []string) bool

CanSortBy returns true, if all requested sortBy arguments can be used for sorting This is not the case for a virtual column, which has no underlying value type

func FilterSortableColumns added in v0.11.0

func FilterSortableColumns[T any](cols columns.ColumnMap[T], sortBy []string) ([]string, []string)

FilterSortableColumns returns two lists, one containing the valid column names and another containing the invalid column names.

func SortEntries

func SortEntries[T any](cols columns.ColumnMap[T], entries []*T, sortBy []string)

SortEntries sorts entries by applying the sortBy rules from right to left (first rule has the highest priority). The rules are strings containing the column names, optionally prefixed with "-" to switch to descending sort order.

Types

type ColumnSorterCollection

type ColumnSorterCollection[T any] struct {
	// contains filtered or unexported fields
}

func Prepare

func Prepare[T any](cols columns.ColumnMap[T], sortBy []string) *ColumnSorterCollection[T]

Prepare prepares a sorter collection that can be re-used for multiple calls to Sort() for efficiency. Filter rules will be applied from right to left (first rule has the highest priority).

func (*ColumnSorterCollection[T]) Sort

func (csc *ColumnSorterCollection[T]) Sort(entries []*T)

Jump to

Keyboard shortcuts

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