Once a CSV
file is loaded, as below, how is the data then sorted?
PS /home/nicholas/powershell/csv>
PS /home/nicholas/powershell/csv> $words = Import-Csv -Path ./sort.csv
PS /home/nicholas/powershell/csv>
PS /home/nicholas/powershell/csv> $words
symbol code word morse code phonetic
------ --------- ---------- --------
A Alfa/Alpha ● ▬ AL FAH
B Bravo ▬ ● ● ● BRAH VOH
C Charlie ▬ ● ▬ ● CHAR LEE
D Delta ▬ ● ● DELL TAH
E Echo .● ECK OH
F Foxtrot ● ● ▬ ● FOKS TROT
G Golf ▬ ▬ ● GOLF
Z Zulu ▬ ▬ ▬ ▬ ▬ ZOO LOO
H Hotel ● ● ● ● HOH TELL
I India ● ● IN DEE AH
J Juliett ● ▬ ▬ ▬ JEW LEE ETT
K Kilo ▬ ● ▬ KEY LOH
L Lima ● ▬ ● ● LEE MAH
M Mike ▬ ▬ MIKE
N November ▬ ● NO VEMBER
O Oscar ▬ ▬ ▬ OSS CAH
I India ● ● IN DEE AH
P Papa ● ▬ ▬ ● PAH PAH
Q Quebec ▬ ▬ ● ▬ KEH BECK
S Sierra ● ● ● SEE AIRRAH
R Romeo ● ▬ ● ROW ME OH
T Tango ▬ TANG OH
U Uniform ● ● ▬ YOU NEE FORM
V Victor ● ● ● ▬ VIK TAH
W Whiskey ● ▬ ▬ WISS KEY
X X-ray ▬ ● ● ▬ ECKS RAY
Y Yankee ▬ ▬ ● ● YANG KEY
Z Zulu ▬ ▬ ▬ ▬ ▬ ZOO LOO
PS /home/nicholas/powershell/csv>
PS /home/nicholas/powershell/csv> $words | sort 'code word'
/usr/bin/sort: cannot read: 'code word': No such file or directory
PS /home/nicholas/powershell/csv>
PS /home/nicholas/powershell/csv> sort $words
/usr/bin/sort: cannot read: '@{symbol=A; code word=Alfa/Alpha; morse code=● ▬; phonetic=AL FAH}': No such file or directory
PS /home/nicholas/powershell/csv>
PS /home/nicholas/powershell/csv> help sort
PS /home/nicholas/powershell/csv>
see also:
https://devblogs.microsoft.com/scripting/use-powershell-to-sort-csv-files-in-order/
which has similar examples, except that the data is sorted as its imported.
original data as:
nicholas@mordor:~/powershell/csv$
nicholas@mordor:~/powershell/csv$ head sort.csv
symbol,code word,morse code,phonetic
A,Alfa/Alpha,● ▬,AL FAH
B,Bravo,▬ ● ● ●,BRAH VOH
C,Charlie,▬ ● ▬ ●,CHAR LEE
D,Delta,▬ ● ●,DELL TAH
E,Echo,.●,ECK OH
F,Foxtrot,● ● ▬ ●,FOKS TROT
G,Golf,▬ ▬ ●,GOLF
Z,Zulu,▬ ▬ ▬ ▬ ▬,ZOO LOO
H,Hotel,● ● ● ●,HOH TELL
nicholas@mordor:~/powershell/csv$
which was copy/pasted from a website.
get members shows:
PS /home/nicholas/powershell/csv>
PS /home/nicholas/powershell/csv> $words = Import-Csv -Path ./sort.csv
PS /home/nicholas/powershell/csv>
PS /home/nicholas/powershell/csv> $words | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
code word NoteProperty string code word=Alfa/Alpha
morse code NoteProperty string morse code=● ▬
phonetic NoteProperty string phonetic=AL FAH
symbol NoteProperty string symbol=A
PS /home/nicholas/powershell/csv>
which is as expected.
sort
is an external command (Application) on Linux systems.
In other words, do not use the short name () but the full cmdlet name sort
Sort-Object
:
$Words |Sort-Object 'code word'
As commented by @postanote, details about the command precedence can be found in about_Command_Precedence. Reading this, might actually look confusing as the process is defined to occur in this order: Alias, Function, Cmdlet, any external command/script
(which should give the alias precedence over the external command sort
).
Apparently the sort
alias is simply not installed on Linux systems (presumably to prevent it from overruling the external native Linux command):
Get-Command sort
CommandType Name Version Source
----------- ---- ------- ------
Alias sort -> Sort-Object
Get-Command sort
CommandType Name Version Source
----------- ---- ------- ------
Application sort 0.0.0.0 /usr/bin/sort
This means that you might also resolve this issue by manually overruling the external sort
command with a sort
alias:
Set-Alias sort Sort-Object