As @Lee_Dailey pointed out, the simplest solution would be casting the strings to the type System.Version:
PS /home/me> $s1 = '1.2.3'
PS /home/me> $s2 = '1.19.2'
PS /home/me> [Version]$v1 = $s1
PS /home/me> [Version]$v2 = $s2
PS /home/me> $v1
Major Minor Build Revision
----- ----- ----- --------
1 2 3 -1
PS /home/me> $v2
Major Minor Build Revision
----- ----- ----- --------
1 19 2 -1
PS /home/me> $v1 -gt $v2
False
PS /home/me> $v2 -gt $v1
True
I just tested this in PowerShell Core 6.1 on Linux and it worked just fine, so I'd expect it to work in PowerShell Core on Mac OS X as well.
However, you could also use the exact same approach you're using in Python: create tuples from the strings.
PS /home/me> $s1 = '1.2.3'
PS /home/me> $s2 = '1.19.2'
PS /home/me> [int[]]$v1 = $s1.Split('.')
PS /home/me> [int[]]$v2 = $s2.Split('.')
PS /home/me> $t1 = New-Object 'Tuple[int,int,int]' $a1
PS /home/me> $t2 = New-Object 'Tuple[int,int,int]' $a2
PS /home/me> $t1
Item1 Item2 Item3 Length
----- ----- ----- ------
1 2 3 3
PS /home/me> $t2
Item1 Item2 Item3 Length
----- ----- ----- ------
1 19 2 3
PS /home/me> $t1 -gt $t2
False
PS /home/me> $t2 -gt $t1
True
Note that splitting the string will give you a string array, so you MUST cast that to an integer array, otherwise the comparisons won't work correctly (as string comparisons rather than numeric comparisons would be used).
Note also that the number of elements in the type definition (Tuple[<type>,<type>,...]) MUST match the number of elements in the array from which the tuple is created. This answer shows a reusable function for creating tuples from arbitrary arrays:
function New-Tuple {
Param(
[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true
)]
[ValidateCount(2,20)]
[array]$Values
)
Process {
$types = ($Values | ForEach-Object { $_.GetType().Name }) -join ','
New-Object "Tuple[$types]" $Values
}
}
so you could change
$t1 = New-Object 'Tuple[int,int,int]' $a1
$t2 = New-Object 'Tuple[int,int,int]' $a2
to
$t1 = New-Tuple $a1
$t2 = New-Tuple $a2
Beware, though, that comparing tuples requires them to have the same number of elements, otherwise the comparison will fail:
PS /home/me> $s3 = '1.19.2.1'
PS /home/me> [int[]]$a3 = $s3.Split('.')
PS /home/me> $t3 = New-Object 'Tuple[int,int,int,int]' $a3
PS /home/me> $t3 -gt $t2
Could not compare "(1, 19, 2, 1)" to "(1, 19, 2)". Error: "Cannot convert the
"(1, 19, 2)" value of type "System.Tuple`3[[System.Int32, System.Private.CoreLib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32,
System.Private.CoreLib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]" to type
"System.Tuple`4[System.Int32,System.Int32,System.Int32,System.Int32]"."
At line:1 char:1
+ $t3 -gt $t2
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : ComparisonFailure
So you must ensure that the version tuples always have the same length, e.g. by appending 0 elements to the array and then picking the first 4 elements from the result:
[int[]]$a = ('1.2'.Split('.') + (0, 0, 0, 0))[0..3]
$t = New-Object 'Tuple[int,int,int,int]' $a
The [Version] type accelerator does not have this issue, because it creates objects of the same type, where missing numbers in a version string are automatically filled with the value -1.
PS /home/me> [Version]'1.2'
Major Minor Build Revision
----- ----- ----- --------
1 2 -1 -1
PS /home/me> [Version]'1.2.3'
Major Minor Build Revision
----- ----- ----- --------
1 2 3 -1
PS /home/me> [Version]'1.2.3.4'
Major Minor Build Revision
----- ----- ----- --------
1 2 3 4
[version]dotnet type instead? it understands that1.2.3.4is less than1.19.2.4.[system.version]type in dotnet core ... but the search engine used refuses to search for that exact phrase - only for those two words. ///// you may want to look at "semantic version" over at the powershell gallery. this looks interesting >> PowerShell Gallery | PoshSemanticVersion 1.5.1 — powershellgallery.com/packages/PoshSemanticVersion/1.5.1