Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a column in a PowerShell table disappear if I sort in the opposite order?

Tags:

powershell

I want to produce a table of process names and total CPU times sorted by total CPU time. The following works as expected:

ps | sort -p cpu | select processname,cpu

But if I reverse the direction of sorting as follows, the CPU time column disappears:

ps | sort -descending -p cpu | select processname,cpu

Why is this?

It seems that the CPU property is sometimes a Double and sometimes null. When I ran the first command, the first item had null CPU and the column does get displayed: for the second command, the first item has a Double-valued CPU and the column doesn't get displayed.

When the column doesn't get displayed it still exists! Using Format-List, shows it, for example:

ps | sort -descending -p cpu | select processname,cpu | fl

What is going on?

like image 827
Omar Antolín-Camarena Avatar asked Jun 27 '11 01:06

Omar Antolín-Camarena


People also ask

How do I sort in ascending order in PowerShell?

The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values. If sort properties aren't included in a command, PowerShell uses default sort properties of the first input object.

How do you sort data in PowerShell?

To sort the output in the PowerShell you need to use Sort-Object Pipeline cmdlet. In the below example, we will retrieve the output from the Get-Process command and we will sort the, according to memory and CPU usage.

How do I sort an array in PowerShell?

Sort array: It's very easy of arranging the elements of an array in a order with PowerShell. Just we need to do is pipe the output of an array to the Sort-Object cmdlet: The default sort order is ascending : the numbers range from small to large. To perform a descending sort requires utilizing the Descending switch.


1 Answers

Try this

ps | sort -descending -p cpu | select processname,cpu | Format-Table -AutoSize
like image 191
Doug Finke Avatar answered Nov 15 '22 03:11

Doug Finke