sort

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Numbers

func Numbers(input string) (numbers []float64)

Numbers extracts any number of floating point values from a string.

This is similar to [strconv.ParseFloat()], however:

  • Numbers never returns an error
  • Numbers returns a []float64 slice of all individual numbers found within the string

This is used for numeric sorting. The typical string collator provided by text/collate only identifies integer numbers within strings.

By extracting all floating point numbers in each string, psv can sort scientific notation and even semver version numbers (with multiple '.'s, e.g. `v1.2.3`)

See also VersionNumbers

func SortColumns

func SortColumns(colFlags []model.ColumnFlags) (sortCols []int)

SortColumns prerpares a sorted list of column indexes, in the order that they should be used when comparing rows.

There are two basic scenarios:

Default, Left to Right Sorting :

  • no columns have any sorting flags the indexes [0, 1, 2, 3, ...] is returned

Configured Sorting:

  • when some columns do have sorting flags
  • only columns with the SortColumnFlag set are included
  • the columns are sorted by their sortpriority

func SortRows

func SortRows(rows [][]string, opts ...SortOption) [][]string

SortRows sorts a table of rows by comparing the values of each column in turn.

This sorter does NOT work in-place! A shallow copy of the rows parameter is returned.

Example:

fmt.Println("unsorted:\n",data)
SortRows(data)
fmt.Println("\nsorted:\n",data)

Might produce output similar to this:

unsorted:
| group | member | role      |
| ----- | ------ | --------- |
| a     | Alice  | Leader    |
| b     | Bob    | secretary |
| a     | Anika  |           |
| b     | Becky  |           |
| a     | Axel   |           |

sorted:
| group | member | role      |
| ----- | ------ | --------- |
| a     | Alice  | Leader    |
| a     | Anika  |           |
| a     | Axel   |           |
| b     | Becky  |           |
| b     | Bob    | secretary |

func VersionNumbers added in v0.3.2

func VersionNumbers(input string) (nums []float64)

VersionNumbers extracts any number of integer values from a string, returning the numbers as floats.

This is a much simpler version of the, more generic, Numbers function, suitable for parsing version numbers.

Accepted Formats:

  • unsigned sequences of [0-9] only

Notes:

  • the first number in the returned slice ENDS at the position of the version index in the string.

Types

type SortOption

type SortOption func(*sortConfig)

SortOption provides a generic interface for settion optional configuration parameters

Example:

SortRows( data, WithColumns( []int{1,3,2}) )

func WithCollator

func WithCollator(collator StringCollator) SortOption

WithCollator provides a non-standard string comparison function with the signature

func WithColumnFlags

func WithColumnFlags(colFlags []model.ColumnFlags) SortOption

WithColumnFlags provides an optional set of additional flags for sorting each column.

If no model.ColumnFlags are provided, then the rows are sorted in ascending order by comparing each column using the StringCollator.

If any ColumnFlags are provided, then only the columns which have the model.SortColumnFlag set are used for sorting.

func WithColumns

func WithColumns(sortCols []int) SortOption

WithColumns provides a list of column indexes to be compared, in order, when comparing rows.

By default, rows are compared by comparing each of their fields, from first to last.

Example:

SortRows( data, WithColumns( []int{3,2}) )

would sort the data by comparing each row's 3rd and 2nd columns only, in that order.

func WithLocaleTag

func WithLocaleTag(localeTag string) SortOption

WithLocaleTag provides the user's preferred locale, which is used to get the default StringCollator

func WithRowSpans

func WithRowSpans(rowSpans []int) SortOption

WithRowSpans provides a list of ints used to segregate groups for sorting.

Each span is simply the number of rows to sort as a group.

By default, all rows are sorted as a single group.

Example:

A table may have multiple horizontal rulers, in which each ruler defines a separate group:

| group | member | role      |
| ----- | ------ | --------- |
| a     | Anika  |           |
| a     | Axel   |           |
| a     | Alice  | Leader    |
| ----- | ------ | --------- |
| b     | Bob    | secretary |
| b     | Becky  |           |

Thus to sort this table, we should provide the spans 1, 3 and 2

SortRows( data, WithRowSpans( []int{1,3,2}) )

The resulting, sorted table would be:

| group | member | role      |
| ----- | ------ | --------- |
| a     | Alice  | Leader    |
| a     | Anika  |           |
| a     | Axel   |           |
| ----- | ------ | --------- |
| b     | Becky  |           |
| b     | Bob    | secretary |

type StringCollator

type StringCollator interface {
	CompareString(a, b string) int
}

StringCollator interface is used for comparing strings when sorting a table.

The StringCollator _only_ performs the string comparison.

Numeric columns are always sorted by their numeric values first, and only use the collator to resolve equal comparisons

Jump to

Keyboard shortcuts

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