sort

For filters.

Sort the input in increasing order.

Command Type

built-in

Signature

> sort {flags}

Flags

  • --reverse, -r: Sort in reverse order.
  • --ignore-case, -i: Sort string-based data case-insensitively.
  • --values, -v: If input is a single record, sort the record by values; ignored if input is not a single record.
  • --natural, -n: Sort alphanumeric string-based values naturally (1, 9, 10, 99, 100, …).

Input/output types:

input output
list<any> list<any>
record record

Examples

Sort the list by increasing value.

> [2 0 1] | sort
╭───┬───╮
│ 0 │ 0 │
│ 1 │ 1 │
│ 2 │ 2 │
╰───┴───╯

Sort the list by decreasing value.

> [2 0 1] | sort --reverse
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 1 │
│ 2 │ 0 │
╰───┴───╯

Sort a list of strings.

> [betty amy sarah] | sort
╭───┬───────╮
│ 0 │ amy   │
│ 1 │ betty │
│ 2 │ sarah │
╰───┴───────╯

Sort a list of strings in reverse.

> [betty amy sarah] | sort --reverse
╭───┬───────╮
│ 0 │ sarah │
│ 1 │ betty │
│ 2 │ amy   │
╰───┴───────╯

Sort strings (case-insensitive).

> [airplane Truck Car] | sort -i
╭───┬──────────╮
│ 0 │ airplane │
│ 1 │ Car      │
│ 2 │ Truck    │
╰───┴──────────╯

Sort strings (reversed case-insensitive).

> [airplane Truck Car] | sort -i -r
╭───┬──────────╮
│ 0 │ Truck    │
│ 1 │ Car      │
│ 2 │ airplane │
╰───┴──────────╯

Sort alphanumeric strings in natural order.

> [foo1 foo10 foo9] | sort -n
╭───┬───────╮
│ 0 │ foo1  │
│ 1 │ foo9  │
│ 2 │ foo10 │
╰───┴───────╯

Sort record by key (case-insensitive).

> {b: 3, a: 4} | sort
╭───┬───╮
│ a │ 4 │
│ b │ 3 │
╰───┴───╯

Sort record by value.

> {b: 4, a: 3, c:1} | sort -v
╭───┬───╮
│ c │ 1 │
│ a │ 3 │
│ b │ 4 │
╰───┴───╯

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.