简体   繁体   English

在Powershell中对路径列表进行排序

[英]Sorting a list of paths in Powershell

I am trying to sort (from deepest folder to root) a list of given paths. 我试图排序(从最深的文件夹到根)一个给定路径的列表。

Is there a way to achieve this with existing functions? 有没有办法通过现有功能实现这一目标?

Example: 例:

Given: 鉴于:

test\A\directory1
test\B
test\A\directory1\end
test\A
test\C\directory2
test
test\C
test\directdirectory

To obtain: 获得:

test\C\directory2
test\A\directory1
test\directdirectory
test\C
test\B
test\A
test

You can use an expression in your sort command to sort by the amount of \\ 您可以在sort命令中使用表达式按数量排序\\

Sort {($_ -split '\\').Count}, {$_} -Descending

Example kudos to LotPings 实施例 荣誉给LotPings

@(
'test\A\directory1'
'test\B'
'test\A\directory1\end'
'test\A'
'test\C\directory2'
'test'
'test\C'
'test\directdirectory'
) | Sort {($_ -split '\\').Count}, {$_} -Descending

Result 结果

test\A\directory1\end
test\C\directory2
test\A\directory1
test\directdirectory
test\C
test\B
test\A
test

Edit: is sorting on the second key necessary the jury is still out on that 编辑:正在对第二把钥匙进行排序, 陪审团仍然在那里

排序第二个关键差异

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

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