在 PowerShell 上查找 Hashtable 的最小元素(值为 - DateTime)
Find the min element of Hashtable (Values are - DateTime) on PowerShell
有一个视图的哈希表:
键(字符串)-值(日期时间)
必须在 Values (dateTime-s) 中找到最小值。
无法找到通用方法来找到这样的值。唯一的办法就是
$first_file_date = $dates_hash.Values | Measure-Object -Minimum -Maximum
Get-Date ($first_file_date);
虽然我得到了结果 ($first_file_date),但实际值转换为 GenericObjectMeasureInfo 类型,我无法将其转换回 DateTime 以进一步工作。
有什么想法吗?
您感兴趣的值存储在 Measure-Object
返回的对象的 Minimum
和 Maximum
属性中:
$measurement = $dates_hash.Values | Measure-Object -Minimum -Maximum
# Minimum/oldest datetime value is stored here
$measurement.Minimum
# Maximum/newest datetime value is stored here
$measurement.Maximum
如果您希望单个管道中的原始值,请使用 ForEach-Object
或 Select-Object
:
$oldest = $dates_hash.Values | Measure-Object -Minimum | ForEach-Object -MemberName Minimum
# or
$oldest = $dates_hash.Values | Measure-Object -Minimum | Select-Object -ExpandProperty Minimum
为此使用 Sort-Object
:
$dates_hash = @{
"a" = (Get-Date).AddMinutes(4)
"b" = (Get-Date).AddMinutes(5)
"c" = (Get-Date).AddMinutes(2)
"d" = (Get-Date).AddMinutes(5)
"e" = (Get-Date).AddMinutes(1)
"f" = (Get-Date).AddMinutes(6)
"g" = (Get-Date).AddMinutes(8)
}
$first_file_date = $dates_hash.Values | Sort-Object | Select-Object -First 1
或者如果你想要整个对象:
$first_file = $dates_hash.GetEnumerator() | Sort-Object -Property "Value" | Select-Object -First 1
用基于 LINQ 的替代解决方案补充 :
# Sample hashtable.
$hash = @{
foo = (Get-Date)
bar = (Get-Date).AddDays(-1)
}
# Note that the minimum is sought among the hash's *values* ([datetime] instances)
# The [datetime[] cast is required to find the appropriate generic overload.
[Linq.Enumerable]::Min([datetime[]] $hash.Values)
使用 LINQ from PowerShell is generally cumbersome, unfortunately (see ). GitHub proposal #2226 提出改进建议。
有一个视图的哈希表: 键(字符串)-值(日期时间)
必须在 Values (dateTime-s) 中找到最小值。 无法找到通用方法来找到这样的值。唯一的办法就是
$first_file_date = $dates_hash.Values | Measure-Object -Minimum -Maximum
Get-Date ($first_file_date);
虽然我得到了结果 ($first_file_date),但实际值转换为 GenericObjectMeasureInfo 类型,我无法将其转换回 DateTime 以进一步工作。
有什么想法吗?
您感兴趣的值存储在 Measure-Object
返回的对象的 Minimum
和 Maximum
属性中:
$measurement = $dates_hash.Values | Measure-Object -Minimum -Maximum
# Minimum/oldest datetime value is stored here
$measurement.Minimum
# Maximum/newest datetime value is stored here
$measurement.Maximum
如果您希望单个管道中的原始值,请使用 ForEach-Object
或 Select-Object
:
$oldest = $dates_hash.Values | Measure-Object -Minimum | ForEach-Object -MemberName Minimum
# or
$oldest = $dates_hash.Values | Measure-Object -Minimum | Select-Object -ExpandProperty Minimum
为此使用 Sort-Object
:
$dates_hash = @{
"a" = (Get-Date).AddMinutes(4)
"b" = (Get-Date).AddMinutes(5)
"c" = (Get-Date).AddMinutes(2)
"d" = (Get-Date).AddMinutes(5)
"e" = (Get-Date).AddMinutes(1)
"f" = (Get-Date).AddMinutes(6)
"g" = (Get-Date).AddMinutes(8)
}
$first_file_date = $dates_hash.Values | Sort-Object | Select-Object -First 1
或者如果你想要整个对象:
$first_file = $dates_hash.GetEnumerator() | Sort-Object -Property "Value" | Select-Object -First 1
用基于 LINQ 的替代解决方案补充
# Sample hashtable.
$hash = @{
foo = (Get-Date)
bar = (Get-Date).AddDays(-1)
}
# Note that the minimum is sought among the hash's *values* ([datetime] instances)
# The [datetime[] cast is required to find the appropriate generic overload.
[Linq.Enumerable]::Min([datetime[]] $hash.Values)
使用 LINQ from PowerShell is generally cumbersome, unfortunately (see