【问题标题】:Does Powershell Sort-Object use a stable sortPowershell Sort-Object 是否使用稳定的排序
【发布时间】:2012-07-06 00:10:50
【问题描述】:

我需要从多个来源创建一个不同的历史项目列表,其中最新的项目是保留的项目。我正在尝试这样的事情,但由于问题域,检查结果是否准确并不容易。

Function GetUnique {
    param( $source1, $source2, ...)

    $items = $source1 + $source2 + ...;

    $unique = $items | Sort-Object -Desc -Unique -Prop Timestamp;

    # Does this contain the most recent items?

    $unique;
}

我很惊讶没有 -Stable 开关来指示偏好。

注意:我知道即使排序是稳定的,我也会对唯一性算法做出假设。如果排序是稳定的,我可以相对容易地编写我自己的稳定的 Get-Unique 命令行开关,假设稳定的排序输入。但是,我真的不想实现 MergeSort。

【问题讨论】:

  • 我猜 PowerShell 内部只使用 List.Sort 或 LINQ 的 OrderBy(至少我们在 Pash 中是这样做的),因此实际算法甚至可能会根据 .NET 版本而改变。

标签: sorting powershell stable-sort


【解决方案1】:

所以在放弃了我的场景之后,我找到了一个简单的测试来表明排序实际上并不稳定,但是以某种稳定的方式颠倒了序列。请注意,我使用的测试集非常小,因此这些结果可能不确定,但可以重现。

function f ([String] $name, [int] $value) `
{
    return New-Object PSObject -Property @{ Name=$name; Value=$value } | 
        select Name,Value; 
};
$test = (f a 1),(f a 2),(f a 3),(f b 1),(f b 2);
"`n`$test;"
$test;
"`n`$test | sort name;"
$test | sort name;
"`n`$test | sort name -Desc;"
$test | sort name -Desc;
"`n`$test | sort name | sort name;"
$test | sort name | sort name;
"`n`$test | sort value | sort name;"
$test | sort value | sort name;
"`n`$test | sort value;"
$test | sort value;

结果如下:

$test;
Name                                                  Value
----                                                  -----
a                                                         1
a                                                         2
a                                                         3
b                                                         1
b                                                         2

$test | sort name;
a                                                         3
a                                                         2
a                                                         1
b                                                         2
b                                                         1

$test | sort name -Desc;
b                                                         1
b                                                         2
a                                                         1
a                                                         2
a                                                         3

$test | sort name | sort name;
a                                                         1
a                                                         2
a                                                         3
b                                                         1
b                                                         2

$test | sort value | sort name;
a                                                         2
a                                                         1
a                                                         3
b                                                         1
b                                                         2

$test | sort value;
b                                                         1
a                                                         1
b                                                         2
a                                                         2
a                                                         3

我已通过https://connect.microsoft.com/PowerShell/feedback/details/752455/provide-stable-switch-for-sort-object-cmdlet 向 PS 团队提交了一个建议。如果您同意,请点赞。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多