0

I have a variable $try that contains an array, and I want $res to contain the sorted version of $try. This seems easy enough, but I can't get this to work when $try happens to contain an empty array.

When $try contains a non-empty array, no problem:

PS C:\data> $try = @("a", "c", "b")

PS C:\data> $res = $try | Sort-Object

PS C:\data> $res
a
b
c

PS C:\data> $res.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                                                                                 
-------- -------- ----                                     --------                                                                                                                                                                                                                                                 
True     True     Object[]                                 System.Array

However, when the input array is empty, the output object is $null, not (as I would have expected) an empty array:

PS C:\data> $try = @()

PS C:\data> $res = $try | Sort-Object

PS C:\data> $res

PS C:\data> $null -eq $res
True

PS C:\data> $null -eq $try
False

I want $res to be the sorted version of $try, even if $try happens to contain an empty array. In that case, I want $res to also be an empty array.

Is there a way of doing this other than something like:

PS C:\data> $try = @()

PS C:\data> if ($try) {$res = $try | Sort-Object} else { $res = @() }

PS C:\data> $null -eq $try
False

PS C:\data> $null -eq $res
False
0

2 Answers 2

3

You can force $res to be an array like this, even when $try is $null or an empty array:

$try = $null  # or an empty array @()
$res = @($try | Sort-Object)

# $res is now an array with 0 elements
$res.GetType()

Result

IsPublic IsSerial Name      BaseType                                                                                   
-------- -------- ----      --------                                                                                   
True     True     Object[]  System.Array
Sign up to request clarification or add additional context in comments.

1 Comment

I think I'm still missing some basic PowerShell knowledge, because my initial reading of @($try | Sort-Object) leads me to think that this would result in an array that has a single member, namely the sorted array. But since your suggestion works, the result of $try | Sort-Object must not be an array itself, so I have some PowerShell learning to do it seems. :) (But it does surprise me a bit that a gazillion "How to sort an array in PowerShell" blog posts don't mention this, and just pipe the unsorted array to Sort-Object).
1

You can initialise your res variable and append to res your result sort :

$try = @()
$res=@()
$res += $try | Sort-Object

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.