title: Powershell Multi-dimensional arrays sorting
body: <p>I have a script which processes all our SQL Servers. The script has several functions and I have an error routine which records the server name, function name and error mesage into a 500 row x 3 column array. At the end of the script I want to sort this array into server name sequence. Several posts have suggested all I need to do is pipe the array to the <code>sort-object</code> cmdlet, but when I do this every element of my array is replaced with <code>system.Object[]</code>.  NB. The array filling before the function is just an example of what my array looks like</p>
  
  <pre><code>$global:ErrorCount = 0
  $global:ErrArray = new-object 'object[,]' 500,3
  
  $global:ErrArray[1,00]= "SV000004"
  $global:ErrArray[1,01]= "ProcessServers"
  $global:ErrArray[1,02]= "The server was not found or was not accessible."
  
  $global:ErrArray[2,00]= "BOSWEB02"
  $global:ErrArray[2,01]= "GetDatabases"
  $global:ErrArray[2,02]= "Database Status = Shutdown"
  
  $global:ErrArray[3,00]= "SATURN"
  $global:ErrArray[3,01]= "GetDatabases"
  $global:ErrArray[3,02]= "Database Status = Shutdown"
  
  $global:ErrArray[4,00]= "BOSWEB02"
  $global:ErrArray[4,01]= "GetSystemInfo"
  $global:ErrArray[4,02]= "Access is denied"
  
  $global:ErrorCount = 4
  
  Function DisplayErrors
  {
      Write-Host "`nBefore:-`n"
  
      for ( $iLoop=1; $iLoop -le $global:ErrorCount; $iLoop++)
      {
          "{0,-14}  {1,-18}  {2,-80}" -f 
            $global:ErrArray[$iLoop,0], $global:ErrArray[$iLoop,1], 
            $global:ErrArray[$iLoop,2]
      }
  
      $Sorted = $global:ErrArray | Sort-Object @{Expression={$_[0]}}
  
      Write-Host "`nAfter:-`n"    
  
      for ( $iLoop=1; $iLoop -le $global:ErrorCount; $iLoop++)
      {
          "{0,-14}  {1,-18}  {2,-80}" -f 
            $Sorted[$iLoop,0], $Sorted[$iLoop,1], $Sorted[$iLoop,2]
      }
  }  
  
  DisplayErrors
  </code></pre>
  
  <p>Output looks like this:-</p>
  
  <p>Before:-</p>
  
  <pre><code>SV000004        ProcessServers      The server was not found or was not accessible.                                 
  BOSWEB02        GetDatabases        Database Status = Shutdown                                                      
  SATURN          GetDatabases        Database Status = Shutdown                                                      
  BOSWEB02        GetSystemInfo       Access is denied                                                                
  </code></pre>
  
  <p>After:-</p>
  
  <pre><code>System.Object[]  System.Object[]     System.Object[]                                                                 
  System.Object[]  System.Object[]     System.Object[]                                                                 
  System.Object[]  System.Object[]     System.Object[]                                                                 
  System.Object[]  System.Object[]     System.Object[]  
  </code></pre>
  
  <p>Can anyone tell me what I am doing wrong here ?</p>
  
  <p>Many thanks :-)</p>