Skip to content
shellmap

sort-objectSort pipeline objects by property across all 5 shells

Equivalents in every shell

Bashunix
command | sort -k3 -n -r

`sort` operates on text lines, picking a column with `-k<N>` (1-indexed, space-delimited by default). `-n` numeric, `-r` reverse, `-u` unique. For mixed-type sorts (numeric AND alphabetic columns) chain multiple `-k` keys: `sort -k1,1 -k3,3n`. Column boundaries depend on `$IFS` / `-t<delim>` — fragile vs. PowerShell objects which carry typed properties.

Zshunix
command | sort -k3 -n -r

Same `sort` binary. Zsh adds parameter expansion sort flags — `${(o)array}` ascending, `${(O)array}` descending, `${(on)array}` numeric ascending — handy when you already have an array variable and want to avoid forking sort.

Fishunix
command | sort -k3 -n -r

Same external `sort`. Fish has no built-in sort, but its arrays preserve insertion order (no associative-array surprise) so piping to external `sort` and capturing back into `set --` is the usual idiom.

PowerShellwindows
Get-Process | Sort-Object CPU -Descending

PowerShell-native cmdlet (alias `sort`). Sorts on object PROPERTIES (typed comparison, no text parsing). Multiple keys: `Sort-Object CPU,Name`. Stable sort (PS 7+). Custom keys via hashtables: `Sort-Object @{Expression="Name"; Descending=$true}, @{Expression="CPU"; Ascending=$true}`. `-Unique` removes duplicates by sort key.

cmd.exewindows
sort /+10 /R input.txt

cmd `sort` is a text-only utility — `/+N` sorts starting at column N (1-indexed), `/R` reverse, `/U` unique. No multi-key, no per-column type. For object-aware sorting (process list by CPU, files by size) you need PowerShell. `dir /o` has its own ordering flags (`/od` date, `/os` size, `/on` name, `-` for reverse).

Worked examples

Sort processes by memory usage, highest first

Bash
ps -eo rss,comm --sort=-rss
PowerShell
Get-Process | Sort-Object WorkingSet64 -Descending

Sort files in the current directory by size, smallest first

Bash
ls -lS -r
Fish
ls -lS -r
PowerShell
Get-ChildItem -File | Sort-Object Length
cmd.exe
dir /os

Sort by two keys — primary by name, secondary by CPU descending

Bash
ps -eo comm,pcpu | sort -k1,1 -k2,2nr
PowerShell
Get-Process | Sort-Object Name, @{Expression='CPU';Descending=$true}

Gotchas

  • `Sort-Object` compares using the property's `IComparable` implementation — string properties sort lexicographically, so a `Version` string like `'1.10.0'` sorts BEFORE `'1.2.0'`. For semantic version sort, cast: `Sort-Object { [Version]$_.Version }`.
  • `Sort-Object` materialises the ENTIRE pipeline before emitting (it cannot stream — sorting requires all input). On a huge input stream this can blow memory; pre-filter with `Where-Object` or `Select-Object -First N` (after sort) is fine, but `Select-Object -First N` BEFORE the sort defeats the sort. For very large inputs, sort upstream (`Get-Content file.txt | Sort-Object` on a 10GB file = 10GB resident).
  • `Sort-Object -Unique` uses the sort KEY for equality — `Sort-Object Name -Unique` keeps one row per Name (drops other rows). It does NOT respect other properties. For full-row dedup use `Sort-Object | Get-Unique` (which compares using the object's `Equals` method).
  • Stable sort: PowerShell 7+ `Sort-Object` is stable (equal-key items preserve input order); PowerShell 5.1 (Windows) is NOT stable. Scripts that rely on stable sort (e.g., for tie-break behaviour) need to chain a secondary key, or upgrade to pwsh 7.
  • Numeric vs string sort: `Sort-Object` automatically picks numeric comparison if the property is a numeric type, lexicographic if it's a string. Comparing strings that look numeric (`'10' vs '9'` from a CSV import) sorts lexicographically (9 > 10). Cast: `Sort-Object { [int]$_.Count }`.

WSL & PowerShell Core notes

pwsh`Sort-Object` is identical on every pwsh platform. The `sort` alias is REMOVED on Linux/macOS pwsh so the system `/usr/bin/sort` resolves first — `command | sort` on Linux pwsh calls GNU sort (text-line sort), not the cmdlet (object sort). For cross-platform scripts, always spell `Sort-Object`.

Common tasks using sort-object

Related commands