PowerShell Sort-Object是否使用稳定排序?

3

我需要从多个来源创建一个独特的历史记录项目列表,其中最近的项目是保留的。我正在尝试类似于以下内容的操作,但由于问题领域的原因,检查结果是否准确并不简单。

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 版本而改变。 - Joey
1个回答

4

所以在放弃我的方案后,我找到了一个简单的测试来展示排序其实并不稳定,但在某种程度上会以一种稳定的方式颠倒顺序。需要注意的是,我使用的测试数据集非常小,因此这些结果可能不确定,但是可以重现。

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

我已经向 PS 团队提交了一个有关此问题的建议,链接为:https://connect.microsoft.com/PowerShell/feedback/details/752455/provide-stable-switch-for-sort-object-cmdlet。如果您同意,请点赞支持。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接