Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting PowerShell versions

In PowerShell, if I have a list of strings containing versions, "3.0.1.1", "3.2.1.1", etc., how can I sort it the way System.Version would sort it in C#?

like image 931
maxfridbe Avatar asked Apr 02 '09 18:04

maxfridbe


People also ask

How do I sort PowerShell results?

To sort the output in the PowerShell you need to use Sort-Object Pipeline cmdlet. In the below example, we will retrieve the output from the Get-Process command and we will sort the, according to memory and CPU usage.

Which switch sorts output from highest to lowest?

By default, the Sort-Object cmdlet performs an ascending sort—the numbers range from small to large. To perform a descending sort requires utilizing the Descending switch. Note: There is no Ascending switch for the Sort-Object cmdlet because that is the default behavior.

How do I sort an array of numbers in PowerShell?

There are actually two ways to do this. The first way to do this is to use the Sort-Object cmdlet (Sort is an alias for the Sort-Object cmdlet). The second way to sort an array is to use the static Sort method from the System.


2 Answers

PS C:\> $ver="3.0.1.1","3.2.1.1"
PS C:\> $ver|%{[System.Version]$_}|sort

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      1      1
3      2      1      1
like image 182
James Pogran Avatar answered Nov 16 '22 01:11

James Pogran


Just convert it to a Version and sort that way:

$list = "3.0.1.1","3.2.1.1" 
$sorted = $list | %{ new-object System.Version ($_) } | sort
like image 24
JaredPar Avatar answered Nov 16 '22 00:11

JaredPar