sort-object — Sort pipeline objects by property across all 5 shells
Equivalents in every shell
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.
command | sort -k3 -n -rSame `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.
command | sort -k3 -n -rSame 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.
Get-Process | Sort-Object CPU -DescendingPowerShell-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.
sort /+10 /R input.txtcmd `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
ps -eo rss,comm --sort=-rssGet-Process | Sort-Object WorkingSet64 -DescendingSort files in the current directory by size, smallest first
ls -lS -rls -lS -rGet-ChildItem -File | Sort-Object Lengthdir /osSort by two keys — primary by name, secondary by CPU descending
ps -eo comm,pcpu | sort -k1,1 -k2,2nrGet-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 }`.