We help IT Professionals succeed at work.

Sort version numbers - Powershell

mikeydk
mikeydk asked
on
367 Views
Last Modified: 2017-12-04
Hey

How do I sort version numbers: (in an array)

1.1.0.8400
1.10.0.14127
1.10.1.14414
1.11.0.14649
1.11.1.14865
1.12.0.15302
1.12.1.15602
1.12.1.15662
1.2.0.9064
1.4.0.10352
1.5.0.11365
1.5.0.11534
1.6.0.12058
1.7.0.12519
1.7.1.12780
1.8.0.13165
1.9.0.13640
1.9.1.13731
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
You may not even have to cast the value as a version
$data | sort

Open in new window

CERTIFIED EXPERT
Top Expert 2014

Commented:
@aikimark - no that doesn't work because with string sorting you will get results like 1.12.1.15662 before 1.2.0.9064.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
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).
CERTIFIED EXPERT
Top Expert 2014

Commented:
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.