Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cmd/helpers/sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

var DefaultSortFlag = &cli.StringFlag{
Name: "sort",
Usage: "Sorting method for display (value, text, numeric, contextual, date)",
Usage: "Sorting method for display in format `key:order`. Keys: (v)alue, (t)ext, (n)umeric, (c)ontextual, (d)ate; Orders: (a)scending, (d)escending, (r)everse",
Value: "numeric",
}

Expand Down Expand Up @@ -60,15 +60,15 @@ func parseSort(name string) (realname string, reverse bool, err error) {
}

realname = strings.ToLower(splitter.Next())
reverse = (realname == "value") // Value defaults descending
reverse = (realname == "value" || realname == "val" || realname == "v") // Value defaults descending

if modifier, hasModifier := splitter.NextOk(); hasModifier {
switch strings.ToLower(modifier) {
case "rev", "reverse":
case "rev", "reverse", "r":
reverse = !reverse
case "desc":
case "desc", "descending", "d":
reverse = true
case "asc":
case "asc", "ascending", "a":
reverse = false
default:
return "", false, errors.New("invalid sort modifier")
Expand All @@ -81,15 +81,15 @@ func parseSort(name string) (realname string, reverse bool, err error) {
func lookupSorter(name string) (sorting.NameValueSorter, error) {
name = strings.ToLower(name)
switch name {
case "text", "":
case "text", "t", "":
return sorting.ValueNilSorter(sorting.ByName), nil
case "numeric":
case "numeric", "n":
return sorting.ValueNilSorter(sorting.ByNameSmart), nil
case "contextual", "context":
case "contextual", "context", "c":
return sorting.ValueNilSorter(sorting.ByContextual()), nil
case "date":
case "date", "d":
return sorting.ValueNilSorter(sorting.ByDateWithContextual()), nil
case "value":
case "value", "val", "v":
return sorting.ValueSorterEx(sorting.ByName), nil
}
return nil, errors.New("unknown sort")
Expand Down
6 changes: 6 additions & 0 deletions cmd/helpers/sorting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func TestOrderResults(t *testing.T) {
assertSortEquals(t, "value:desc", 3, 2, 1, 0, 4)
assertSortEquals(t, "value:reverse", 4, 0, 1, 2, 3)
assertSortEquals(t, "value:asc", 4, 0, 1, 2, 3)

// Shorthands
assertSortEquals(t, "v", 3, 2, 1, 0, 4)
assertSortEquals(t, "val", 3, 2, 1, 0, 4)
assertSortEquals(t, "n:r", 3, 0, 2, 4, 1)
assertSortEquals(t, "t:d", 3, 0, 2, 4, 1)
}

func TestInvalidSortNames(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions docs/usage/aggregators.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ Many of the aggregators support changing the order in which the data is displaye
can change this from default either by setting the `--sort` flag or `--sort-rows` and `--sort-cols`
flags for tables.

These are the supported sorters:
These are the supported sorters. You can shorthand to the first letter.

* `text` -- Pure alphanumeric sort. Fastest, but can sort numbers oddly (eg. would sort 1, 11, 2, ...)
* `numeric` -- Attempts to parse the value as numeric. If unable to parse, falls
Expand All @@ -352,7 +352,7 @@ These are the supported sorters:

In addition to the sorting method, you can also modify the sort by adding a colon and the modifier, eg: `numeric:desc`

These are the supported modifiers:
These are the supported modifiers. You can shorthand to the first letter.

* `:reverse` -- Reverse of the "default"
* `:asc` -- Ascending order
Expand Down
Loading