title: powershell: import-csv | get-member: sort column names / property names based on (original) position. rename colnames?
body: <p>The get-member commandlet returns NoteProperties always sorted alphanumerically. See sample output below.</p>
  
  <p>From import-csv I received an array in memory, now I want get-member to sort the member names by original position rather than its alphanumerical value.</p>
  
  <p>The original sequence is visible in the $_.psextended.Definition string ( column names joined by commas)</p>
  
  <p>I cannot in-place edit the property names, as it's read-only.
  As I workaround I tried to prepend a numeric prefix to the column name, see code below</p>
  
  <p>Any better ideas?
  I don't want to in-place edit the original data file.</p>
  
  <pre><code> $content = (import-csv -delimiter "`t" $infile_abs  );
   $colnames = $content | get-Member |  where-object {$_.MemberType -eq "NoteProperty"} ; #| out-gridview; 
   $cols = $content | get-Member -force | where-object {$_.Name -eq "psextended"} ; 
   echo($cols.Definition -replace "psextended" , "");
  
   $i = 0;
   $colnames| sort-object  -descending |  foreach-object { 
     $i++ ; 
     $cn = [string]::Format( "{0:00}_{1}",   $i, $_.Name )  ;
     Write-Host  $cn
   }; 
  </code></pre>
  
  <p>Sample Output of psextended</p>
  
  <blockquote>
    <p>{File Name, Label, ObsDateTime,
    Port#, Obs#, Exp_Flux, IV Cdry, IV
    Tcham, IV Pressure, IV H2O, IV V3, IV
    V4, IV RH}</p>
  </blockquote>
  
  <p>Output of  $colnames = $content | get-Member |  out-gridview; </p>
  
  <pre><code>Exp_Flux    NoteProperty    System.String Exp_Flux=0,99 
  File Name   NoteProperty    System.String File Name=xxx-11040   
  IV Cdry NoteProperty    System.String IV Cdry=406.96    
  IV H2O  NoteProperty    System.String IV H2O=9.748  
  IV Pressure NoteProperty    System.String IV Pressure=100.7 
  IV RH   NoteProperty    System.String IV RH=53.12   
  IV Tcham    NoteProperty    System.String IV Tcham=16.19    
  IV V3   NoteProperty    System.String IV V3=11.395  
  IV V4   NoteProperty    System.String IV V4=0.759   
  Label   NoteProperty    System.String Label=5m  
  Obs#    NoteProperty    System.String Obs#=1    
  ObsDateTime NoteProperty    System.String ObsDateTime=2011-04-04 13:19:37   
  Port#   NoteProperty    System.String Port#=1
  </code></pre>
  
  <p><strong>EDIT: (No answers yet)</strong></p>
  
  <p>Here is a custom sorting function, now I need to tell Get-Member to use this sorting function. How to do this in a pipeline?</p>
  
  <pre><code> #$orig_seq = $cols.Definition -replace "psextended", "" -replace "[{}]", "";
   $orig_seq = "File Name, Label, ObsDateTime, Port#, Obs#, Exp_Flux, IV Cdry, IV Tcham, IV Pressure, IV H2O, IV V3, IV V4, IV RH";
  
   echo $orig_seq;
   #exit;
  
    &amp;{
  
     $byPos= @{};
     $i = 0; 
     $orig_seq.Split(",") | % { $byPos[$i++] = $_.trim()}
     $order = ([int[]] $byPos.keys) | sort
     #$order | %{ ([string]::Format( "{0} =&gt; {1}",  $_, $byPos[$_])) }
     $order | %{ $byPos[$_] }
  
   }
  </code></pre>