VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

How to sort multiple objects

Hi,

I am unable to sort multiple objects from below script. Please help.

Get-VMHost | Select Name,
@{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},
@{N='CPU GHz Free %';E={[math]::Round(((($_.CpuTotalMhz - $_.CpuUsageMhz) / $_.CpuTotalMhz * 100)),2)}},
@{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},
@{N='Memory Free %';E={[math]::Round((($_.MemoryTotalGB - $_.MemoryUsageGB)/ $_.MemoryTotalGB),2)*100}} | Sort -Property @{E="Name"; Ascending = $True}, @{E="CPU GHz Free %"; Descending = $true}, @{E= "Memory Free %"; Descending = $true} | ft -auto

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You have to explicitly convert those 2 values to Float to have the numerical order.
Something like this

Get-VMHost | Select-Object Name,
@{N = 'CPU GHz Capacity'; E = { [math]::Round($_.CpuTotalMhz / 1000, 2) } },
@{N = 'CPU GHz Free %'; E = { [math]::Round(((($_.CpuTotalMhz - $_.CpuUsageMhz) / $_.CpuTotalMhz * 100)), 2) } },
@{N = 'Memory Capacity GB'; E = { [math]::Round($_.MemoryTotalGB, 2) } },
@{N = 'Memory Free %'; E = { [math]::Round((($_.MemoryTotalGB - $_.MemoryUsageGB) / $_.MemoryTotalGB), 2) * 100 } } |
Sort-Object -Property @{E = 'Name'; Ascending = $True },
  @{E = {[float]$_.'CPU GHz Free %'}; Descending = $true },
  @{E = {[float]$_.'Memory Free %'}; Descending = $true } |
Format-Table -AutoSize


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You have to explicitly convert those 2 values to Float to have the numerical order.
Something like this

Get-VMHost | Select-Object Name,
@{N = 'CPU GHz Capacity'; E = { [math]::Round($_.CpuTotalMhz / 1000, 2) } },
@{N = 'CPU GHz Free %'; E = { [math]::Round(((($_.CpuTotalMhz - $_.CpuUsageMhz) / $_.CpuTotalMhz * 100)), 2) } },
@{N = 'Memory Capacity GB'; E = { [math]::Round($_.MemoryTotalGB, 2) } },
@{N = 'Memory Free %'; E = { [math]::Round((($_.MemoryTotalGB - $_.MemoryUsageGB) / $_.MemoryTotalGB), 2) * 100 } } |
Sort-Object -Property @{E = 'Name'; Ascending = $True },
  @{E = {[float]$_.'CPU GHz Free %'}; Descending = $true },
  @{E = {[float]$_.'Memory Free %'}; Descending = $true } |
Format-Table -AutoSize


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Tried but it is sorting only the Name but it is not sorting the other objects.

ganapa2000_0-1660889964615.png

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is not the output my snippet generates, you have more columns.
What code are you using?

Just tested my snippet again, and for me it is workingsort.png


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Here is the output where I am unable to sort objects

ganapa2000_0-1660899547020.png

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I was put on the wrong foot as well.
The Sort-Object with multiple properties does the folowing

When you specify multiple properties, the objects are sorted by the first property. If multiple objects have the same value for the first property, those objects are sorted by the second property. This process continues until there are no more specified properties or no groups of objects.

So what you see is correct.
The lines are sorted on the Name, and only when there are entries with the same name will the next property be used.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Oh. Got it LucD.

Thanks for the detailed info.

0 Kudos