PowerShell equivalent to sort|uniq -c|sort -nr

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
HorstMeier
Posts: 2
Last visit: Fri Oct 21, 2016 9:05 am

PowerShell equivalent to sort|uniq -c|sort -nr

Post by HorstMeier »

Hello guys,

I'm looking for a PowerShell one liner doing the same as "sort | uniq -c | sort -nr" on a Unix shell.

Using for example
  • @(55,22,33,44,11,44,33,22,11,33,22,11,22,11,11) | group-object | format-table -h count,name
will deliver
  • 1 55
    4 22
    3 33
    2 44
    5 11
But the goal is to have this list sorted by frequency in descending order.

Trying
  • @(55,22,33,44,11,44,33,22,11,33,22,11,22,11,11) | group-object | format-table -h count,name | sort-object -descending
doesn't work.

Using a temporary file will do the job but this is not an elegant solution.
  • @(55,22,33,44,11,44,33,22,11,33,22,11,22,11,11) | group-object | format-table -h count,name >tmp
    get-content tmp | sort-object -descending

    5 11
    4 22
    3 33
    2 44
    1 55
Anybody to propose a clean and straightforward way to execute such task in PowerShell just like in the Unix shell?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell equivalent to sort|uniq -c|sort -nr

Post by jvierra »

@(55,22,33,44,11,44,33,22,11,33,22,11,22,11,11) | group-object |sort name -desc
User avatar
HorstMeier
Posts: 2
Last visit: Fri Oct 21, 2016 9:05 am

Re: PowerShell equivalent to sort|uniq -c|sort -nr

Post by HorstMeier »

jvierra wrote:@(55,22,33,44,11,44,33,22,11,33,22,11,22,11,11) | group-object |sort name -desc
No.

But

@(55,22,33,44,11,44,33,22,11,33,22,11,22,11,11) | group-object | sort count -desc | format-table count, name

will do.

Got some hint on powershell dot org.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell equivalent to sort|uniq -c|sort -nr

Post by jvierra »

I left the format table piece for you to figure out. I am surprised you didn't know how t do that.
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked