PowerTip: Use PowerShell to Sort Array

PowerTip: Use PowerShell to Sort Array

  • Comments 4
  • Likes

Summary: Use Windows PowerShell to sort an array.

Hey, Scripting Guy! Question How can I use Windows PowerShell to sort an array?

Hey, Scripting Guy! Answer Use the static Sort method from the [array] class, for example:

$a = 1,2,4,5,3,6

  [array]::sort($a)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • thanks

  • Instead, you can use native powershell cmdlets:

    1,2,4,5,3,6 | Sort-Object

  • It looks like using [array]::sort is about a half a millisecond faster on this small array. It looks like it scales about the same to a larger subset of data ~15000 items as well.

  • Amazing. thank you!