在 powershell 中使用二进制比较函数对集合进行排序
Sort collection with binary comparison function in powershell
有没有办法对提供自定义二进制比较函数的 PowerShell 集合进行排序?
我正在寻找 C# 的类似物 Sort(Comparison) which can be invoked with an instance of delegate int Comparison(T x, T y), or Java's static Collections.sort(List list, Comparator c) where an instance of interface Comparator 可以提供。
顺便说一句:我的问题仅限于 PowerShell v4.0。
编辑:
提供示例集合:
$coll = @{
"A" = @();
"B" = @("C", "D");
"C" = @("E");
"D" = @();
"E" = @();
"F" = @()
}
我的用例需要来自散列 table $coll
的键列表,但以这样的方式排序,即没有键 $must_follow
出现在另一个键 $must_precede
之前这样 $must_precede -in $coll[$must_follow]
.
处理后的预期顺序可能如下所示:
$solved = @{
"A" = @();
"E" = @();
"C" = @("E");
"D" = @();
"B" = @("C", "D");
"F" = @()
}
考虑到上述限制,我没有找到使用任何排序功能的方法。我假设在 PowerShell v4 中没有使用二进制比较器排序这样的功能。
我能够使用不那么花哨的解决方案按要求对我的列表进行排序,或多或少像这样:
- 遍历 $coll 条目
- 迭代条目值中的所有项目
- 使用 $coll
中那些值的对应值递归调用上述检查
- 如果名称不存在,请将名称添加到 $solved
有没有办法对提供自定义二进制比较函数的 PowerShell 集合进行排序? 我正在寻找 C# 的类似物 Sort(Comparison) which can be invoked with an instance of delegate int Comparison(T x, T y), or Java's static Collections.sort(List list, Comparator c) where an instance of interface Comparator 可以提供。
顺便说一句:我的问题仅限于 PowerShell v4.0。
编辑: 提供示例集合:
$coll = @{
"A" = @();
"B" = @("C", "D");
"C" = @("E");
"D" = @();
"E" = @();
"F" = @()
}
我的用例需要来自散列 table $coll
的键列表,但以这样的方式排序,即没有键 $must_follow
出现在另一个键 $must_precede
之前这样 $must_precede -in $coll[$must_follow]
.
处理后的预期顺序可能如下所示:
$solved = @{
"A" = @();
"E" = @();
"C" = @("E");
"D" = @();
"B" = @("C", "D");
"F" = @()
}
考虑到上述限制,我没有找到使用任何排序功能的方法。我假设在 PowerShell v4 中没有使用二进制比较器排序这样的功能。
我能够使用不那么花哨的解决方案按要求对我的列表进行排序,或多或少像这样:
- 遍历 $coll 条目
- 迭代条目值中的所有项目
- 使用 $coll 中那些值的对应值递归调用上述检查
- 如果名称不存在,请将名称添加到 $solved