Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

sort-by for filters

Sort by the given cell path or closure.

Signature

> sort-by {flags} ...rest

Flags

  • --reverse, -r: Sort in reverse order
  • --ignore-case, -i: Sort string-based data case-insensitively
  • --natural, -n: Sort alphanumeric string-based data naturally (1, 9, 10, 99, 100, ...)
  • --custom, -c: Use closures to specify a custom sort order, rather than to compute a comparison key

Parameters

  • ...rest: The cell path(s) or closure(s) to compare elements by.

Input/output types:

inputoutput
list<any>list<any>
recordtable
tabletable

Examples

Sort files by modified date

> ls | sort-by modified

Sort files by name (case-insensitive)

> ls | sort-by name --ignore-case

Sort a table by a column (reversed order)

> [[fruit count]; [apple 9] [pear 3] [orange 7]] | sort-by fruit --reverse
╭───┬────────┬───────╮
│ # │ fruit  │ count │
├───┼────────┼───────┤
│ 0 │ pear   │     3 │
│ 1 │ orange │     7 │
│ 2 │ apple  │     9 │
╰───┴────────┴───────╯

Sort by a nested value

> [[name info]; [Cairo {founded: 969}] [Kyoto {founded: 794}]] | sort-by info.founded
╭───┬───────┬───────────────────╮
│ # │ name  │       info        │
├───┼───────┼───────────────────┤
│ 0 │ Kyoto │ ╭─────────┬─────╮ │
│   │       │ │ founded │ 794 │ │
│   │       │ ╰─────────┴─────╯ │
│ 1 │ Cairo │ ╭─────────┬─────╮ │
│   │       │ │ founded │ 969 │ │
│   │       │ ╰─────────┴─────╯ │
╰───┴───────┴───────────────────╯

Sort by the last value in a list

> [[2 50] [10 1]] | sort-by { last }
╭───┬────────────╮
│ 0 │ ╭───┬────╮ │
│   │ │ 0 │ 10 │ │
│   │ │ 1 │  1 │ │
│   │ ╰───┴────╯ │
│ 1 │ ╭───┬────╮ │
│   │ │ 0 │  2 │ │
│   │ │ 1 │ 50 │ │
│   │ ╰───┴────╯ │
╰───┴────────────╯

Sort in a custom order

> [7 3 2 8 4] | sort-by -c {|a, b| $a < $b}
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 3 │
│ 2 │ 4 │
│ 3 │ 7 │
│ 4 │ 8 │
╰───┴───╯