简体   繁体   English

PowerShell:按名称升序对对象进行排序

[英]PowerShell: Sort-Object by Name Ascending

PowerShell doesn't seem to sort objects by name ascending correctly: PowerShell 似乎没有正确按名称升序对对象进行排序:

dir | Sort-Object Name

在此处输入图片说明

My goal is to sort the elements in this order:我的目标是按以下顺序对元素进行排序:

1.sql
2.sql
3.sql
...
9.sql
10.sql
11.sql
...

Is there a way to solve this?有没有办法解决这个问题?

You need to sort the filenames as numbers, rather than as text.您需要将文件名排序为数字,而不是文本。 It's convenient because the filenames in your example are entirely numbers, so you can change the sort to use a dynamic scriptblock property, which will evaluate the filename to a number for each item in the pipeline:这很方便,因为您的示例中的文件名完全是数字,因此您可以更改排序以使用动态脚本块属性,这会将文件名评估为管道中每个项目的数字:

| sort-object -Property {
    if (($i = $_.BaseName -as [int])) { $i } else { $_ }
}

That means: if the filename can be converted to an integer, then use that, otherwise use it as it is.这意味着:如果文件名可以转换为整数,则使用它,否则使用它。

For more complex patterns enclosed in alphabetic characters use $ToNatural which expands all embedded numbers to a unique length (here 20) by left padding with zeros.对于包含在字母字符中的更复杂的模式,使用$ToNatural将所有嵌入的数字扩展到唯一的长度(此处为 20),通过左填充零。

Source: Roman Kuzmin - How to sort by file name the same way Windows Explorer does?来源: Roman Kuzmin - 如何像 Windows 资源管理器一样按文件名排序?

$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20,"0") }) }

Generate some test data:生成一些测试数据:

> (10..11 + 100..101 + 1..2)|%{new-item -itemtype file -path ("pre_{0}_post.sql" -f $_)
    Directory: A:\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-07-27     11:02              0 pre_10_post.sql
-a----       2018-07-27     11:02              0 pre_11_post.sql
-a----       2018-07-27     11:02              0 pre_100_post.sql
-a----       2018-07-27     11:02              0 pre_101_post.sql
-a----       2018-07-27     11:02              0 pre_1_post.sql
-a----       2018-07-27     11:02              0 pre_2_post.sql

> dir| sort
    Directory(: A:\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-07-27     11:02              0 pre_1_post.sql
-a----       2018-07-27     11:02              0 pre_10_post.sql
-a----       2018-07-27     11:02              0 pre_100_post.sql
-a----       2018-07-27     11:02              0 pre_101_post.sql
-a----       2018-07-27     11:02              0 pre_11_post.sql
-a----       2018-07-27     11:02              0 pre_2_post.sql

> dir|sort $ToNatural
    Directory: A:\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-07-27     11:02              0 pre_1_post.sql
-a----       2018-07-27     11:02              0 pre_2_post.sql
-a----       2018-07-27     11:02              0 pre_10_post.sql
-a----       2018-07-27     11:02              0 pre_11_post.sql
-a----       2018-07-27     11:02              0 pre_100_post.sql
-a----       2018-07-27     11:02              0 pre_101_post.sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM