2

If I have a multiarray in PowerShell with one entry and I will sort the array it will change the number of lines from 1 to 3. But if I have more than one entry in the multiarray, it will stay the same number of lines after sorting.

PS C:\Users\user> $a = ,("2", "A", "Urs")

PS C:\Users\user> $a
2
A
Urs

PS C:\Users\user> $a.Count
1

PS C:\Users\user> $a = $a | Sort-Object

PS C:\Users\user> $a
2
A
Urs

PS C:\Users\user> $a.Count
3

PS C:\Users\user> $a = ,("2", "A", "Urs")

PS C:\Users\user> $a.Count
1

PS C:\Users\user> $a += ,("3", "B", "Max")

PS C:\Users\user> $a
2
A
Urs
3
B
Max

PS C:\Users\user> $a.Count
2

PS C:\Users\user> $a = $a | Sort-Object

PS C:\Users\user> $a.Count
2
arco444
  • 22,002
  • 12
  • 63
  • 67
Christian Frei
  • 471
  • 4
  • 18
  • Well written question but I'm voting to close as it has [already been asked](http://stackoverflow.com/questions/40954626/sorting-a-2d-array-with-one-row) and there are a number of solutions in the comments and answers – arco444 Jan 11 '17 at 10:02

3 Answers3

0

It stands to reason

$a = ,("2", "A", "Urs")
$a.Count

above $a is multiarray with only one element in it so the count 1

below $a is array with three elements

$a = $a | Sort-Object
$a.Count
Chandan Rai
  • 9,879
  • 2
  • 20
  • 28
  • Yes, but why will it Change the Multiarray to a "normal" Array, because I'm sorting it? I think it should sort the arrays in the arrays. If I have more than two element in the multiarray it will count two, and not six after sorting. – Christian Frei Jan 11 '17 at 08:46
  • the problem here is with the Sort-Object, it works fine with single arrays. but for a multidimensional array you need to do something like $a | Sort-Object @{Expression={$_[1]}; Ascending=$True} | %{ "$($_[0]),$($_[1])" } – Chandan Rai Jan 11 '17 at 08:59
  • But after I sort it like that I will have an string and not an array again. – Christian Frei Jan 11 '17 at 09:34
0

Sorry, I haven't got enought reputation to add a comment instead of answer.

All your problems happend because of pipeline. Pipelines gather their results from a stream. From the stream you can’t tell whether the pipeline returned a singleton or a collection. So you 2-dimensional array with one element becomes 1-dimensional.

Ann Sorokina
  • 171
  • 5
  • Declaration of array is wrongly written. It is still a string. Please be specific to answer always. :) – Ranadip Dutta Jan 11 '17 at 09:08
  • 1
    Please, could you explain what is wrong with array declaration? As I can see type of $a and type of $a[0] are Array. So we really have 2-dimensional array. But pipe (Sort-Object) breaks this because of features of its implementation. – Ann Sorokina Jan 11 '17 at 09:15
  • 1
    Its a 2-dimensional Array. You can test its with $a[0][0] but after sorting its only posible to $a[0] – Christian Frei Jan 11 '17 at 09:21
  • @AnnSorokina : Array declaration should enclose with @(). And if you are still thinking you are getting the data in the first index because PS have the capacity to get the content in an array for a particular variable. thats why 0th index is giving you the result. – Ranadip Dutta Jan 11 '17 at 09:45
  • @ChristianFrei: No it is never a 2 dimensional array and it is not C which will give you the result like this. In PS if its multi-dimensional array, you have to enumerate the elements inside the arraylist. and pick the corresponding value. – Ranadip Dutta Jan 11 '17 at 09:46
  • @ChristianFrei : Although there are ways also where you can pick like that also. Certain exceptions are always there :) – Ranadip Dutta Jan 11 '17 at 09:47
0

I have made a bad workaround, but it works...

$newarray = New-Object System.Collections.ArrayList
$newmultiarray = New-Object System.Collections.ArrayList
$a = ,("test", "3333", "peter", "peter2")
$a += ,("rrrr", "8888", "max", "rhzzh")

$stringlist = $a | Sort-Object @{Expression={$_[0]}; Ascending=$True} | %{ "$($_[0]),$($_[1]),$($_[2]),$($_[3])" }

$arraymulti =$stringlist -split "[\r\n]"

foreach ($i in $arraymulti){
    $array = $i -split ","
        foreach ($y in $array){
            [void]$newarray.Add($y)
        }
    [void]$newmultiarray.Add($newarray)
    $newarray = New-Object System.Collections.ArrayList
    }

$newmultiarray
Christian Frei
  • 471
  • 4
  • 18