1

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.

2
  • 1
    Is your csv file really a csv file? What does $Words | Get-Member return ? Please share your input (csv) file by pasting it (or the first few lines) into the question. Commented Jan 16, 2021 at 12:00
  • I added the head of the file and included get-members output @iRon Commented Jan 16, 2021 at 12:09

1 Answer 1

3

sort is an external command (Application) on Linux systems.
In other words, do not use the short name (sort) but the full cmdlet name 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):

Windows (10)

Get-Command sort

CommandType     Name                                       Version    Source
-----------     ----                                       -------    ------
Alias           sort -> Sort-Object

Linux (Pi OS)

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
Sign up to request clarification or add additional context in comments.

1 Comment

To provide further details, to what iROn's helpful answer, when processing in see about_Command_Precedence. Process occurs in this order, Alias, Function, Cmdlet, any external command/script. The first match wins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.