Why won't PowerShell (v5) sort this array of objects by key?

'[{"key":"f5"},{"key":"f1"},{"key":"f8"}]' | ConvertFrom-Json | sort key
key
---
f5
f1
f8

Other variations tried include ... | Sort-Object -Property {$_.key}

1 Answers

4
Marc On

The answer is that, when piping data directly, each object arrives separately and individually to the Sort-Object cmdlet and it just spits it out.

The solution is to pass the entire array to Sort by using brackets:

('[{"key":"f5"},{"key":"f1"},{"key":"f8"}]' | ConvertFrom-Json) | sort key
key
---
f1
f5
f8