$Oldest = $Results | Where-Object { $_.Type -eq 'Main' } | Sort-Object LastUsed | Select-Object -First 10
Above my command tries to find the 10 oldest dates and then I act on them. My issue is that sometimes the date is blank or $null
How can I push the blank or $null
to the top of the list and find them with the oldest?
CodePudding user response:
The following should place $null
values first in the sort order, before the objects with the oldest dates.
Sort-Object { [bool] $_.LastUsed }, LastUsed
The first Sort-Object
criterion, [bool] $_.LastUsed
, maps $null
values to $false
and non-null values to $true
, and since $false
sorts before $true
, objects with $null
values in their .LastUsed
property are placed first in the sort order.
Those with non-null .LastUsed
values sort according to those values, thanks to the second sort criterion (property).