Sort version numbers - Powershell


How do I sort version numbers: (in an array)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Assuming your array is saved in a variable $data.
$data | sort { [version]$_ }

Open in new window

It's essentially doing a sort based off a calculated property (i.e. the string cast as a version type).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You may not even have to cast the value as a version
$data | sort

Open in new window

@aikimark - no that doesn't work because with string sorting you will get results like before
It depends on the source of the data in the array.  If they went in as versions, they shouldn't need casting.  If they came in as strings, then they will need to be cast as version data, either on input (population of the array) or in the Sort operation (as you've shown).
It's a good point to make that it may be better to cast the data during population of the array.
I assumed that the data in the array is just strings from the way the question was asked, but that could be incorrect.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.