Powershell - 使用特殊字符对字符串对象进行排序

标签 powershell sorting

我在跑

'S-tst','ssrst','srst2','s-zaa','s-a','s-zf' | Sort-Object

我不应该得到返回

s-a
S-tst
s-zaa
s-zf
srst2
ssrst

但我得到以下信息:

s-a
srst2
ssrst
S-tst
s-zaa
s-zf

这怎么可能 ?排序对象在排序时是否只看字母?有什么办法可以按特殊字符排序吗?

最佳答案

这种行为是设计使然,但并不总是人们想要/期望的。如果您希望以 ASCII 顺序使用每个字符对字符串进行排序,请使用以下命令:

Add-Type @"
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;

    public class SimpleStringComparer: IComparer, IComparer<string>
    {

        private static readonly CompareInfo compareInfo = CompareInfo.GetCompareInfo(CultureInfo.InvariantCulture.Name);

        public int Compare(object x, object y)
        {
            return Compare(x as string, y as string);
        }
        public int Compare(string x, string y)
        {
            return compareInfo.Compare(x, y, CompareOptions.OrdinalIgnoreCase);
        }
        public SimpleStringComparer() {}
    }
"@


[string[]]$myList = 's-a','s-a1','s''a','s''a1', 'sa','sa1','s^a','S-a','S-a1','S''a','S''a1', 'Sa','Sa1','S^a'

[System.Collections.Generic.List[string]]$list = [System.Collections.Generic.List[string]]::new()
$list.AddRange($myList)
[SimpleStringComparer]$comparer = [SimpleStringComparer]::new()
$list.Sort([SimpleStringComparer]::new())
$list

输出:

s'a
S'a
s'a1
S'a1
s-a
S-a
s-a1
S-a1
sa
Sa
sa1
Sa1
s^a
S^a

更多信息

根据注释中的@TessellatingHeckler,您可以通过将字符串转换为字符数组来按字符代码(序数)顺序对字符串进行排序。但是,它仍然以一种可能出乎意料的方式处理连字符和撇号(因为这些字符被忽略):

$myList = 's-a','s-a1','s''a','s''a1', 'sa','sa1','s^a','S-a','S-a1','S''a','S''a1', 'Sa','Sa1','S^a'
$myList | Sort-Object -Property { [char[]] $_ }

s'a
S'a
s-a
S-a
s'a1
S'a1
s-a1
S-a1
s^a
S^a
sa
Sa
sa1
Sa1

当前的排序行为是设计使然。 PowerShell 似乎实现了“字排序”。这在此处记录:https://msdn.microsoft.com/en-us/library/windows/desktop/dd318144(v=vs.85).aspx#SortingFunctions

除了忽略连字符和撇号(除非在比较相同的字符串时除外),这种排序还将标点符号视为出现在字母数字之前,并将重音字母与其对应物一起处理。一个简单的演示可以这样看:
32..255 | %{[string][char][byte]$_} | sort

要定义其他排序行为,目前您可能需要深入研究 .Net,如下所示:

Add-Type @"

    using System;
    using System.Runtime.InteropServices;
    using System.Collections;
    public class NumericStringComparer: IComparer
    {
        //https://msdn.microsoft.com/en-us/library/windows/desktop/bb759947%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
        [DllImport("shlwapi.dll")]
        public static extern int StrCmpLogicalW(string psz1, string psz2);
        public int Compare(object x, object y)
        {
            return Compare(x as string, y as string);
        }
        public int Compare(string x, string y)
        {
            return StrCmpLogicalW(x, y);
        }
        public NumericStringComparer() {}
    }

"@

[System.Collections.ArrayList]$myList = 's-a','s-a1','s''a','s''a1', 'sa','sa1','s^a','S-a','S-a1','S''a','S''a1', 'Sa','Sa1','S^a', , '100a','1a','001a','2a','20a'
$myList.Sort([NumericStringComparer]::new())
$myList -join ', '

上面按照 Windows 资源管理器的方式对字符串进行排序(即将前导数字视为数值):

s'a, s'a1, S'a, s-a, S-a, S-a1, S'a1, s-a1, S^a, s^a, 1a, 001a, 2a, Sa, Sa1, sa, sa1, 20a, 100a

我已在 Sort-Object 上提交了功能建议以提供更多对 PS 友好的解决方案.见 https://github.com/PowerShell/PowerShell/issues/4098

关于Powershell - 使用特殊字符对字符串对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731470/

相关文章:

c# - 将 Dictionary[String,String] 从 powershell 发送到 TextTransform.exe

powershell - 使用Powershell通过名称杀死.exe的特定实例

rest - 如何使用 PowerShell 中的 RESTful API 在 Azure 中创建资源组?

html - 使用 powershell 从 HTML 网站抓取图像链接

powershell - Octopus Deploy.ps1 DACPAC 部署失败

java - 对可能包含数字的字符串进行排序

java - hadoop框架上的排序算法

java - 通过复杂比较合并多个排序的 csv 文件

python - 如何使用非标准顺序对 Pandas 中的行进行排序

algorithm - 在线性时间和常数空间中对前 n 个整数进行排序