1

I want to sort array only if it contains more than N elements, something like this:

$myArray | if $myArray.Count() > N -> | Sort-Object

How can I do this in one line?

1 Answer 1

2

You can just use an if statement and do everything in one line:

if ($myArray.Length -gt N) { $myArray = $myArray | Sort-Object }

But why would you wan't to do it? I would prefer it this way:

if ($myArray.Length -gt N) 
{ 
    $myArray = $myArray | Sort-Object 
}

You might wan't to find a solution without an if statement (only pipeline) but I don't see a reason for that.

Sign up to request clarification or add additional context in comments.

Comments

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.