The get-member commandlet returns NoteProperties always sorted alphanumerically. See sample output below.
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.
The original sequence is visible in the $_.psextended.Definition string ( column names joined by commas)
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
Any better ideas? I don't want to in-place edit the original data file.
$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
};
Sample Output of psextended
{File Name, Label, ObsDateTime, Port#, Obs#, Exp_Flux, IV Cdry, IV Tcham, IV Pressure, IV H2O, IV V3, IV V4, IV RH}
Output of $colnames = $content | get-Member | out-gridview;
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
EDIT: (No answers yet)
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?
#$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;
&{
$byPos= @{};
$i = 0;
$orig_seq.Split(",") | % { $byPos[$i++] = $_.trim()}
$order = ([int[]] $byPos.keys) | sort
#$order | %{ ([string]::Format( "{0} => {1}", $_, $byPos[$_])) }
$order | %{ $byPos[$_] }
}