1

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 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm... this looks a bit like reputation farming if you ask a question and post the answer within the same minute..
To be honest, I found the answer while formulating the question and there's always the option to self-answer so I'm trying it out. I had a real question which neither google nor stackoverflow's search was answering (at least not as I had formulated it). Never seen a harvest of this reputation farming of which you speak ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.