问题
What is the default order of result set from Get-ChildItem command in powershell? Is there a order at all or is it just random and would give the results in a different order every time.
回答1:
First folders in alphanumeric order (first numbers as [char]).
Then files in alphanumeric order (first numbers as [char]).
回答2:
There is no guaranteed default sort order. The documentation doesn't state one.
The implementation in FileSystemProvider
ends up calling DirectoryInfo.GetFiles. The documentation for that states
The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.
The underlying API calls are FindFirstFile
and FindNextFile
. Raymond Chen explains here that in practice the file system driver may observe some ordering rules but this should not be relied upon.
回答3:
Sorting by default doesn't work for me. I get:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2015-10-12 10:51 tools
d---- 2015-10-12 10:58 cfg
d---- 2015-10-12 10:51 res
d---- 2015-10-12 10:50 brand
d---- 2015-10-12 10:51 lib
I don't recall changing sort order. I'm on Windows 7 SP1. If I need sorted result I have to do ls | sort
.
来源:https://stackoverflow.com/questions/15807802/what-is-the-default-order-of-result-set-from-get-childitem-command-in-powershell