Sorting a list of paths in Powershell












5















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









share|improve this question























  • 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
















5















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









share|improve this question























  • 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














5












5








5








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












1 Answer
1






active

oldest

votes


















6














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



Sorting on a second key difference






share|improve this answer


























  • Thank you very much, sir! It works perfectly for me

    – Adrian Ileana
    Nov 22 '18 at 12:26






  • 1





    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













  • 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





    @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











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
});


}
});














draft saved

draft discarded


















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









6














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



Sorting on a second key difference






share|improve this answer


























  • Thank you very much, sir! It works perfectly for me

    – Adrian Ileana
    Nov 22 '18 at 12:26






  • 1





    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













  • 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





    @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
















6














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



Sorting on a second key difference






share|improve this answer


























  • Thank you very much, sir! It works perfectly for me

    – Adrian Ileana
    Nov 22 '18 at 12:26






  • 1





    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













  • 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





    @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














6












6








6







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



Sorting on a second key difference






share|improve this answer















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



Sorting on a second key difference







share|improve this answer














share|improve this answer



share|improve this answer








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} -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













  • 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





    @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



















  • Thank you very much, sir! It works perfectly for me

    – Adrian Ileana
    Nov 22 '18 at 12:26






  • 1





    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













  • 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





    @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

















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)