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:
testAdirectory1
testB
testAdirectory1end
testA
testCdirectory2
test
testC
testdirectdirectory
To obtain:
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
powershell sorting arraylist path
add a comment |
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:
testAdirectory1
testB
testAdirectory1end
testA
testCdirectory2
test
testC
testdirectdirectory
To obtain:
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
powershell sorting arraylist path
What is your input? Just a txt file or is it an output from another command?
– James C.
Nov 22 '18 at 12:05
Your given and obtain don't match?!
– Lieven Keersmaekers
Nov 22 '18 at 12:14
add a comment |
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:
testAdirectory1
testB
testAdirectory1end
testA
testCdirectory2
test
testC
testdirectdirectory
To obtain:
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
powershell sorting arraylist path
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:
testAdirectory1
testB
testAdirectory1end
testA
testCdirectory2
test
testC
testdirectdirectory
To obtain:
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
powershell sorting arraylist path
powershell sorting arraylist path
asked Nov 22 '18 at 11:53
Adrian IleanaAdrian Ileana
283
283
What is your input? Just a txt file or is it an output from another command?
– James C.
Nov 22 '18 at 12:05
Your given and obtain don't match?!
– Lieven Keersmaekers
Nov 22 '18 at 12:14
add a comment |
What is your input? Just a txt file or is it an output from another command?
– James C.
Nov 22 '18 at 12:05
Your given and obtain don't match?!
– Lieven Keersmaekers
Nov 22 '18 at 12:14
What is your input? Just a txt file or is it an output from another command?
– James C.
Nov 22 '18 at 12:05
What is your input? Just a txt file or is it an output from another command?
– James C.
Nov 22 '18 at 12:05
Your given and obtain don't match?!
– Lieven Keersmaekers
Nov 22 '18 at 12:14
Your given and obtain don't match?!
– Lieven Keersmaekers
Nov 22 '18 at 12:14
add a comment |
1 Answer
1
active
oldest
votes
You can use an expression in your sort command to sort by the amount of
Sort {($_ -split '\').Count}, {$_} -Descending
Example kudos to LotPings
@(
'testAdirectory1'
'testB'
'testAdirectory1end'
'testA'
'testCdirectory2'
'test'
'testC'
'testdirectdirectory'
) | Sort {($_ -split '\').Count}, {$_} -Descending
Result
testAdirectory1end
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
Edit: is sorting on the second key necessary the jury is still out on that

Thank you very much, sir! It works perfectly for me
– Adrian Ileana
Nov 22 '18 at 12:26
1
Nitpicking:| Sort-Object {($_ -split '\').Count} -Descendinghas the same result. (+1)
– LotPings
Nov 22 '18 at 15:59
@LotPings - It depends on OP's needs but I assumed the resulting groups should also be sorted descending but perhaps you meant shortening the solution and in that case, thank you. I had no idea ;)
– Lieven Keersmaekers
Nov 22 '18 at 18:43
I did test withPSItemas 2nd key (without {scriptblock}) but that had the very same output as my suggestion.
– LotPings
Nov 22 '18 at 19:13
1
@LotPings - It does not for me. Without explicitly sorting on the 2nd key, thetestBcomes beforetestA. Perhaps this is similar to how a database works when you don't specify a sort order: the result can be in any order but most of the time it wil return the results ordered on it's (indexed) primary key.
– Lieven Keersmaekers
Nov 23 '18 at 7:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430460%2fsorting-a-list-of-paths-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use an expression in your sort command to sort by the amount of
Sort {($_ -split '\').Count}, {$_} -Descending
Example kudos to LotPings
@(
'testAdirectory1'
'testB'
'testAdirectory1end'
'testA'
'testCdirectory2'
'test'
'testC'
'testdirectdirectory'
) | Sort {($_ -split '\').Count}, {$_} -Descending
Result
testAdirectory1end
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
Edit: is sorting on the second key necessary the jury is still out on that

Thank you very much, sir! It works perfectly for me
– Adrian Ileana
Nov 22 '18 at 12:26
1
Nitpicking:| Sort-Object {($_ -split '\').Count} -Descendinghas the same result. (+1)
– LotPings
Nov 22 '18 at 15:59
@LotPings - It depends on OP's needs but I assumed the resulting groups should also be sorted descending but perhaps you meant shortening the solution and in that case, thank you. I had no idea ;)
– Lieven Keersmaekers
Nov 22 '18 at 18:43
I did test withPSItemas 2nd key (without {scriptblock}) but that had the very same output as my suggestion.
– LotPings
Nov 22 '18 at 19:13
1
@LotPings - It does not for me. Without explicitly sorting on the 2nd key, thetestBcomes beforetestA. Perhaps this is similar to how a database works when you don't specify a sort order: the result can be in any order but most of the time it wil return the results ordered on it's (indexed) primary key.
– Lieven Keersmaekers
Nov 23 '18 at 7:16
add a comment |
You can use an expression in your sort command to sort by the amount of
Sort {($_ -split '\').Count}, {$_} -Descending
Example kudos to LotPings
@(
'testAdirectory1'
'testB'
'testAdirectory1end'
'testA'
'testCdirectory2'
'test'
'testC'
'testdirectdirectory'
) | Sort {($_ -split '\').Count}, {$_} -Descending
Result
testAdirectory1end
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
Edit: is sorting on the second key necessary the jury is still out on that

Thank you very much, sir! It works perfectly for me
– Adrian Ileana
Nov 22 '18 at 12:26
1
Nitpicking:| Sort-Object {($_ -split '\').Count} -Descendinghas the same result. (+1)
– LotPings
Nov 22 '18 at 15:59
@LotPings - It depends on OP's needs but I assumed the resulting groups should also be sorted descending but perhaps you meant shortening the solution and in that case, thank you. I had no idea ;)
– Lieven Keersmaekers
Nov 22 '18 at 18:43
I did test withPSItemas 2nd key (without {scriptblock}) but that had the very same output as my suggestion.
– LotPings
Nov 22 '18 at 19:13
1
@LotPings - It does not for me. Without explicitly sorting on the 2nd key, thetestBcomes beforetestA. Perhaps this is similar to how a database works when you don't specify a sort order: the result can be in any order but most of the time it wil return the results ordered on it's (indexed) primary key.
– Lieven Keersmaekers
Nov 23 '18 at 7:16
add a comment |
You can use an expression in your sort command to sort by the amount of
Sort {($_ -split '\').Count}, {$_} -Descending
Example kudos to LotPings
@(
'testAdirectory1'
'testB'
'testAdirectory1end'
'testA'
'testCdirectory2'
'test'
'testC'
'testdirectdirectory'
) | Sort {($_ -split '\').Count}, {$_} -Descending
Result
testAdirectory1end
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
Edit: is sorting on the second key necessary the jury is still out on that

You can use an expression in your sort command to sort by the amount of
Sort {($_ -split '\').Count}, {$_} -Descending
Example kudos to LotPings
@(
'testAdirectory1'
'testB'
'testAdirectory1end'
'testA'
'testCdirectory2'
'test'
'testC'
'testdirectdirectory'
) | Sort {($_ -split '\').Count}, {$_} -Descending
Result
testAdirectory1end
testCdirectory2
testAdirectory1
testdirectdirectory
testC
testB
testA
test
Edit: is sorting on the second key necessary the jury is still out on that

edited Nov 23 '18 at 7:22
answered Nov 22 '18 at 12:14
Lieven KeersmaekersLieven Keersmaekers
47k1188124
47k1188124
Thank you very much, sir! It works perfectly for me
– Adrian Ileana
Nov 22 '18 at 12:26
1
Nitpicking:| Sort-Object {($_ -split '\').Count} -Descendinghas the same result. (+1)
– LotPings
Nov 22 '18 at 15:59
@LotPings - It depends on OP's needs but I assumed the resulting groups should also be sorted descending but perhaps you meant shortening the solution and in that case, thank you. I had no idea ;)
– Lieven Keersmaekers
Nov 22 '18 at 18:43
I did test withPSItemas 2nd key (without {scriptblock}) but that had the very same output as my suggestion.
– LotPings
Nov 22 '18 at 19:13
1
@LotPings - It does not for me. Without explicitly sorting on the 2nd key, thetestBcomes beforetestA. Perhaps this is similar to how a database works when you don't specify a sort order: the result can be in any order but most of the time it wil return the results ordered on it's (indexed) primary key.
– Lieven Keersmaekers
Nov 23 '18 at 7:16
add a comment |
Thank you very much, sir! It works perfectly for me
– Adrian Ileana
Nov 22 '18 at 12:26
1
Nitpicking:| Sort-Object {($_ -split '\').Count} -Descendinghas the same result. (+1)
– LotPings
Nov 22 '18 at 15:59
@LotPings - It depends on OP's needs but I assumed the resulting groups should also be sorted descending but perhaps you meant shortening the solution and in that case, thank you. I had no idea ;)
– Lieven Keersmaekers
Nov 22 '18 at 18:43
I did test withPSItemas 2nd key (without {scriptblock}) but that had the very same output as my suggestion.
– LotPings
Nov 22 '18 at 19:13
1
@LotPings - It does not for me. Without explicitly sorting on the 2nd key, thetestBcomes beforetestA. Perhaps this is similar to how a database works when you don't specify a sort order: the result can be in any order but most of the time it wil return the results ordered on it's (indexed) primary key.
– Lieven Keersmaekers
Nov 23 '18 at 7:16
Thank you very much, sir! It works perfectly for me
– Adrian Ileana
Nov 22 '18 at 12:26
Thank you very much, sir! It works perfectly for me
– Adrian Ileana
Nov 22 '18 at 12:26
1
1
Nitpicking:
| Sort-Object {($_ -split '\').Count} -Descending has the same result. (+1)– LotPings
Nov 22 '18 at 15:59
Nitpicking:
| Sort-Object {($_ -split '\').Count} -Descending has the same result. (+1)– LotPings
Nov 22 '18 at 15:59
@LotPings - It depends on OP's needs but I assumed the resulting groups should also be sorted descending but perhaps you meant shortening the solution and in that case, thank you. I had no idea ;)
– Lieven Keersmaekers
Nov 22 '18 at 18:43
@LotPings - It depends on OP's needs but I assumed the resulting groups should also be sorted descending but perhaps you meant shortening the solution and in that case, thank you. I had no idea ;)
– Lieven Keersmaekers
Nov 22 '18 at 18:43
I did test with
PSItem as 2nd key (without {scriptblock}) but that had the very same output as my suggestion.– LotPings
Nov 22 '18 at 19:13
I did test with
PSItem as 2nd key (without {scriptblock}) but that had the very same output as my suggestion.– LotPings
Nov 22 '18 at 19:13
1
1
@LotPings - It does not for me. Without explicitly sorting on the 2nd key, the
testB comes before testA. Perhaps this is similar to how a database works when you don't specify a sort order: the result can be in any order but most of the time it wil return the results ordered on it's (indexed) primary key.– Lieven Keersmaekers
Nov 23 '18 at 7:16
@LotPings - It does not for me. Without explicitly sorting on the 2nd key, the
testB comes before testA. Perhaps this is similar to how a database works when you don't specify a sort order: the result can be in any order but most of the time it wil return the results ordered on it's (indexed) primary key.– Lieven Keersmaekers
Nov 23 '18 at 7:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430460%2fsorting-a-list-of-paths-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is your input? Just a txt file or is it an output from another command?
– James C.
Nov 22 '18 at 12:05
Your given and obtain don't match?!
– Lieven Keersmaekers
Nov 22 '18 at 12:14