Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 有效地寻找极值_Sorting_Powershell_Max - Fatal编程技术网

Sorting 有效地寻找极值

Sorting 有效地寻找极值,sorting,powershell,max,Sorting,Powershell,Max,“如何查找最新文件”的答案是: 这对于大量文件来说是无效的 有没有一种内置的方法可以有效地找到极值?这里有一种方法。函数Max: function Max ($Property) { $max = $null foreach ($elt in $input) { if ($max -eq $null) { $max = $elt } if ($elt.$Property -gt $max.$Property) { $max = $elt

“如何查找最新文件”的答案是:

这对于大量文件来说是无效的


有没有一种内置的方法可以有效地找到极值?

这里有一种方法。函数
Max

function Max ($Property)
{
    $max = $null
    foreach ($elt in $input)
    {
        if ($max -eq $null) { $max = $elt }

        if ($elt.$Property -gt $max.$Property) { $max = $elt }
    }

    $max
}
可用于定义最新的

function Newest () { $input | Max LastWriteTime }
function Largest () { $input | Max Length }
function Min ($Property)
{
    $min = $null
    foreach ($elt in $input)
    {
        if ($min -eq $null) { $min = $elt }

        if ($elt.$Property -lt $min.$Property) { $min = $elt }
    }

    $min
}

function Oldest () { $input | Min LastWriteTime }

function Smallest () { $input | Min Length }
可以这样称呼它:

dir | Newest
它还可用于定义
最大值

function Newest () { $input | Max LastWriteTime }
function Largest () { $input | Max Length }
function Min ($Property)
{
    $min = $null
    foreach ($elt in $input)
    {
        if ($min -eq $null) { $min = $elt }

        if ($elt.$Property -lt $min.$Property) { $min = $elt }
    }

    $min
}

function Oldest () { $input | Min LastWriteTime }

function Smallest () { $input | Min Length }
例如:

类似地,
Min
可用于定义
最早的
最小的

function Newest () { $input | Max LastWriteTime }
function Largest () { $input | Max Length }
function Min ($Property)
{
    $min = $null
    foreach ($elt in $input)
    {
        if ($min -eq $null) { $min = $elt }

        if ($elt.$Property -lt $min.$Property) { $min = $elt }
    }

    $min
}

function Oldest () { $input | Min LastWriteTime }

function Smallest () { $input | Min Length }

这里有一种方法。函数
Max

function Max ($Property)
{
    $max = $null
    foreach ($elt in $input)
    {
        if ($max -eq $null) { $max = $elt }

        if ($elt.$Property -gt $max.$Property) { $max = $elt }
    }

    $max
}
可用于定义最新的

function Newest () { $input | Max LastWriteTime }
function Largest () { $input | Max Length }
function Min ($Property)
{
    $min = $null
    foreach ($elt in $input)
    {
        if ($min -eq $null) { $min = $elt }

        if ($elt.$Property -lt $min.$Property) { $min = $elt }
    }

    $min
}

function Oldest () { $input | Min LastWriteTime }

function Smallest () { $input | Min Length }
可以这样称呼它:

dir | Newest
它还可用于定义
最大值

function Newest () { $input | Max LastWriteTime }
function Largest () { $input | Max Length }
function Min ($Property)
{
    $min = $null
    foreach ($elt in $input)
    {
        if ($min -eq $null) { $min = $elt }

        if ($elt.$Property -lt $min.$Property) { $min = $elt }
    }

    $min
}

function Oldest () { $input | Min LastWriteTime }

function Smallest () { $input | Min Length }
例如:

类似地,
Min
可用于定义
最早的
最小的

function Newest () { $input | Max LastWriteTime }
function Largest () { $input | Max Length }
function Min ($Property)
{
    $min = $null
    foreach ($elt in $input)
    {
        if ($min -eq $null) { $min = $elt }

        if ($elt.$Property -lt $min.$Property) { $min = $elt }
    }

    $min
}

function Oldest () { $input | Min LastWriteTime }

function Smallest () { $input | Min Length }
另一种方法是:

$newest = $null
dir | % { if ($newest -eq $null -or $_.LastWriteTime -gt $newest.LastWriteTime) { $newest = $_ } }
$newest
另一种方法是:

$newest = $null
dir | % { if ($newest -eq $null -or $_.LastWriteTime -gt $newest.LastWriteTime) { $newest = $_ } }
$newest

对于大型目录,我所知道的实现这一点的最快方法是:

(cmd /c dir /b /a-d /tw /od)[-1]

对于大型目录,我所知道的实现这一点的最快方法是:

(cmd /c dir /b /a-d /tw /od)[-1]

还有一些关于.NET程序员的东西::-)


这将返回完整的.NET FileInfo对象。在有限的测试中,它的执行顺序似乎与@mjolinor的解决方案相同。

对于.NET程序员ish:-)


这将返回完整的.NET FileInfo对象。在有限的测试中,它的执行顺序似乎与@mjolinor的解决方案相同。

Nice!(尽管我想我还是会使用我的,如果我需要完整的对象,只需通过Get Item运行结果即可)。@mjolinor您的解决方案肯定少了很多输入,而且更容易使用。:-)只是想在这里宣传LINQ的使用,作为一个有趣的替代品。很好!(尽管我想我还是会使用我的,如果我需要完整的对象,只需通过Get Item运行结果即可)。@mjolinor您的解决方案肯定少了很多输入,而且更容易使用。:-)只是想在这里宣传LINQ的使用,作为一个有趣的替代品。