This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X

# Sorting version numbers

16 replies to this topic

### #1 EvAsion

EvAsion

• Active Members
• 126 posts

Posted 07 October 2007 - 11:42 PM

If i have an array that contains for example:
> 1.4.6.124
> 0.5.24.195
> 1.3.12.1
> 0.55.6.9

How would i go about finding which is the highest? =S

### #2 SmOke_N

SmOke_N

It's not what you know ... It's what you can prove!

• Moderators
• 15,730 posts

Posted 07 October 2007 - 11:44 PM

If i have an array that contains for example:
> 1.4.6.124
> 0.5.24.195
> 1.3.12.1
> 0.55.6.9

How would i go about finding which is the highest? =S

Are these versions?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

### #3 EvAsion

EvAsion

• Active Members
• 126 posts

Posted 07 October 2007 - 11:56 PM

Are these versions?

ya

EDIT: Found _CompareVersion

Edited by EvAsion, 07 October 2007 - 11:57 PM.

### #4 SmOke_N

SmOke_N

It's not what you know ... It's what you can prove!

• Moderators
• 15,730 posts

Posted 07 October 2007 - 11:58 PM

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

### #5 SmOke_N

SmOke_N

It's not what you know ... It's what you can prove!

• Moderators
• 15,730 posts

Posted 08 October 2007 - 12:25 AM

This might be better... I tested it a little bit... maybe someone can confirm that this is really working like I think it is:
```Func _VersionCompare(\$sOne, \$sTwo)
Local \$nCompare = StringCompare(\$sOne, \$sTwo)
If \$nCompare = 0 Then Return 0;Versions are equal
If \$nCompare > 0 Then Return 1;\$sOne is greater
Return -1;\$sTwo is greater
EndFunc```

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

### #6 weaponx

weaponx

I'm coming for blood, no code of conduct, no law.

• MVPs
• 5,366 posts

Posted 08 October 2007 - 01:19 AM

It appears work okay as long as the numbers aren't padded with zeroes.

### #7 SmOke_N

SmOke_N

It's not what you know ... It's what you can prove!

• Moderators
• 15,730 posts

Posted 08 October 2007 - 01:21 AM

It appears work okay as long as the numbers aren't padded with zeroes.

Can you give me an example? I'd match rather use a mathematical way rather than doing it the old way.

Padded... (Only one example):
```\$a = "2.4.5"
\$b = "2.03.05.07"
MsgBox(0, '', _FileCompareVersions(\$a, \$B))
Func _FileCompareVersions(\$sVersion1, \$sVersion2)
Local \$nCompare = StringCompare(\$sVersion1, \$sVersion2)
If \$nCompare = 0 Then Return \$sVersion1
If \$nCompare > 0 Then Return \$sVersion1
Return \$sVersion2
EndFunc```

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

### #8 weaponx

weaponx

I'm coming for blood, no code of conduct, no law.

• MVPs
• 5,366 posts

Posted 08 October 2007 - 12:37 PM

This returns 1 even though they are technically the same in terms of quantity.

```MsgBox(0,"",_VersionCompare("3.2.8.1", "3.2.08.1"))

Func _VersionCompare(\$sOne, \$sTwo)
Local \$nCompare = StringCompare(\$sOne, \$sTwo)
If \$nCompare = 0 Then Return 0;Versions are equal
If \$nCompare > 0 Then Return 1;\$sOne is greater
Return -1;\$sTwo is greater
EndFunc```

Edited by weaponx, 08 October 2007 - 12:37 PM.

### #9 PsaltyDS

PsaltyDS

Most Venerable Penguin

• MVPs
• 13,279 posts

Posted 08 October 2007 - 12:48 PM

[quote name='SmOke_N' post='414642' date='Oct 7 2007, 09:21 PM']Can you give me an example? I'd match rather use a mathematical way rather than doing it the old way.

Padded... (Only one example):
```\$a = "2.4.5"
\$b = "2.03.05.07"
MsgBox(0, '', _FileCompareVersions(\$a, \$B))
Func _FileCompareVersions(\$sVersion1, \$sVersion2)
Local \$nCompare = StringCompare(\$sVersion1, \$sVersion2)
If \$nCompare = 0 Then Return \$sVersion1
If \$nCompare > 0 Then Return \$sVersion1
Return \$sVersion2
\$b = "2.05.05.07"```
May just because it doesn't account for missing parts (one has three parts the other four).

I wrote one a while back to check current driver version against available update driver version, and did it the long way: split each part, pad with trailing zeros so both have the same number of parts, convert to numeric by multiplying each part by 1G, 1M, 1K, 1, and assemble it into a number then do straight numeric compare. You can even have a trailing numeric if you convert that like a=0.1, D=0.4, ad=0.001004. Not short and tight, but very reliable that way.

So 2.4.5 = 2004005000, and 2.03.05.7ad = 2003005007.001004 for the compare.

Don't have the function here, and no time to recode it at the moment so I can't show the demo right now.

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

### #10 weaponx

weaponx

I'm coming for blood, no code of conduct, no law.

• MVPs
• 5,366 posts

Posted 08 October 2007 - 02:00 PM

I threw this together, it's pretty sweet:

AutoIt
```;Return Values
;0 string1 and string2 are equal
;> 0 (1) string1 is greater than string2
;< 0 (-1) string1 is less than string2

;Not required, only used for testing
#include <Array.au3>

MsgBox(0,"",_VersionCompare2("3.2.8", "3.2.8.0")) ;Same
MsgBox(0,"",_VersionCompare2("3.2.8.1", "3.2.8.3")) ; String 2 is greater
MsgBox(0,"",_VersionCompare2("5", "3.2.8.3")) ; String 1 is greater

Func _VersionCompare2(\$sOne, \$sTwo)
;Set default return value to same
Dim \$returnValue = 0

Dim \$arrayOne[4] = [0,0,0,0]
Dim \$arrayTwo[4] = [0,0,0,0]

\$tempOne = StringSplit(\$sOne, ".")
\$tempTwo = StringSplit(\$sTwo, ".")

;Copy anything available from first input version
For \$X = 1 to \$tempOne[0]
If \$X > 4 Then ExitLoop
\$arrayOne[\$X - 1] = Number(\$tempOne[\$X])
Next

;Copy anything available from second input version
For \$X = 1 to \$tempTwo[0]
If \$X > 4 Then ExitLoop
\$arrayTwo[\$X - 1] = Number(\$tempTwo[\$X])
Next

;Compare each individual element instead of whole string
For \$X = 0 to 3
;MsgBox(0,"",StringCompare (\$arrayTwo[\$X],\$arrayTwo[\$X]))
Switch StringCompare (\$arrayOne[\$X],\$arrayTwo[\$X])
Case 0
\$returnValue = 0
Case -1
\$returnValue = -1
ExitLoop
Case 1
\$returnValue = 1
ExitLoop
EndSwitch
Next

;_ArrayDisplay(\$arrayOne)
;_ArrayDisplay(\$arrayTwo)

Return \$returnValue
EndFunc```

### #11 SmOke_N

SmOke_N

It's not what you know ... It's what you can prove!

• Moderators
• 15,730 posts

Posted 14 October 2007 - 12:38 PM

I threw this together, it's pretty sweet:

AutoIt
```;Return Values
;0 string1 and string2 are equal
;> 0 (1) string1 is greater than string2
;< 0 (-1) string1 is less than string2

;Not required, only used for testing
#include <Array.au3>

MsgBox(0,"",_VersionCompare2("3.2.8", "3.2.8.0")) ;Same
MsgBox(0,"",_VersionCompare2("3.2.8.1", "3.2.8.3")) ; String 2 is greater
MsgBox(0,"",_VersionCompare2("5", "3.2.8.3")) ; String 1 is greater

Func _VersionCompare2(\$sOne, \$sTwo)
;Set default return value to same
Dim \$returnValue = 0

Dim \$arrayOne[4] = [0,0,0,0]
Dim \$arrayTwo[4] = [0,0,0,0]

\$tempOne = StringSplit(\$sOne, ".")
\$tempTwo = StringSplit(\$sTwo, ".")

;Copy anything available from first input version
For \$X = 1 to \$tempOne[0]
If \$X > 4 Then ExitLoop
\$arrayOne[\$X - 1] = Number(\$tempOne[\$X])
Next

;Copy anything available from second input version
For \$X = 1 to \$tempTwo[0]
If \$X > 4 Then ExitLoop
\$arrayTwo[\$X - 1] = Number(\$tempTwo[\$X])
Next

;Compare each individual element instead of whole string
For \$X = 0 to 3
;MsgBox(0,"",StringCompare (\$arrayTwo[\$X],\$arrayTwo[\$X]))
Switch StringCompare (\$arrayOne[\$X],\$arrayTwo[\$X])
Case 0
\$returnValue = 0
Case -1
\$returnValue = -1
ExitLoop
Case 1
\$returnValue = 1
ExitLoop
EndSwitch
Next

;_ArrayDisplay(\$arrayOne)
;_ArrayDisplay(\$arrayTwo)

Return \$returnValue
EndFunc```

Ha! I just saw this... Good job.

Something that might make it easier is just to return the version number that is requested (ie... make a param... \$nNewest = 1) > 0 being newest returned string, anything else oldest.

Edit:
Ut Oh

Run this:
MsgBox(0,"",_VersionCompare2("3.2.8.2a", "3.2.8.2b")) ; String 2 is greater

Return Value is 0

Edited by SmOke_N, 14 October 2007 - 12:42 PM.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

### #12 SmOke_N

SmOke_N

It's not what you know ... It's what you can prove!

• Moderators
• 15,730 posts

Posted 14 October 2007 - 12:58 PM

Here try this one...
AutoIt
```MsgBox(0,"",_VersionCompare2("3.2.8", "3.2.8.0")) ;Same
MsgBox(0,"",_VersionCompare2("3.2.8.2c", "3.2.8.2b")) ; String 2 is greater
MsgBox(0,"",_VersionCompare2("5", "3.2.8.3")) ; String 1 is greater

Func _VersionCompare2(\$sOne, \$sTwo, \$nNewer = 1)
;Set default return value to same
Local \$returnValue = 0, \$nCompare
Local \$arrayOne[4] = [0,0,0,0], \$arrayTwo[4] = [0,0,0,0]

Local \$tempOne = StringSplit(\$sOne, "."), \$tempTwo = StringSplit(\$sTwo, ".")

;Copy anything available from first input version
For \$X = 1 to \$tempOne[0]
If \$X > 4 Then ExitLoop
\$arrayOne[\$X - 1] = \$tempOne[\$X]
Next

;Copy anything available from second input version
For \$X = 1 to \$tempTwo[0]
If \$X > 4 Then ExitLoop
\$arrayTwo[\$X - 1] = \$tempTwo[\$X]
Next
;Compare each individual element instead of whole string
For \$X = 0 to 3
;MsgBox(0,"",StringCompare (\$arrayTwo[\$X],\$arrayTwo[\$X]))
Switch StringCompare (\$arrayOne[\$X],\$arrayTwo[\$X])
Case 0
\$returnValue = 0
Case -1
\$returnValue = -1
ExitLoop
Case 1
\$returnValue = 1
ExitLoop
EndSwitch
Next
If \$returnValue > -1 Then Return \$sOne
Return \$sTwo
EndIf
If \$returnValue > -1 Then Return \$sOne
Return \$sTwo
EndFunc```
Should allow for all chars now.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

### #13 weaponx

weaponx

I'm coming for blood, no code of conduct, no law.

• MVPs
• 5,366 posts

Posted 14 October 2007 - 04:57 PM

Hey we were talking about version numbers, not version letters!

I see you removed the Number() function. I think I added that because it would strip off any leading zeros to make the comparison more consistent. Also as far as the return values are concerned I stuck with the StringCompare return values because it's easier to run a case statement or if statement against.

Edited by weaponx, 14 October 2007 - 05:09 PM.

### #14 SmOke_N

SmOke_N

It's not what you know ... It's what you can prove!

• Moderators
• 15,730 posts

Posted 14 October 2007 - 05:14 PM

Hey we were talking about version numbers, not version letters!

I see you removed the Number() function. I think I added that because it would strip off any leading zeros to make the comparison more consistent. Also as far as the return values are concerned I stuck with the StringCompare return values because it's easier to run a case statement or if statement against.

Yeah, I see now the leading zero being an issue again... I have versions of my own apps that have numbers/letters so that's why I thought of it above.

I guess we could just do a regular expression... I'd have to give it some thought... but right now I'm working on 30 hours straight, and not thinking quite clearly, so I could end up making something that reformats your PC for you instead... It will have to wait on my end at least (but I know you're already on it )

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

### #15 weaponx

weaponx

I'm coming for blood, no code of conduct, no law.

• MVPs
• 5,366 posts

Posted 15 October 2007 - 12:41 AM

Well now that we have thoroughly hijacked this thread...

This version will allow alphanumeric and doesn't restrict to a max of 4 octets. Enjoy.

AutoIt
```; Title: VersionCompareX
; Author: WeaponX

;Return Values
;0 string1 and string2 are equal
;1 string1 is greater than string2
;-1 string1 is less than string2

;Compare version numbers with differing lengths
MsgBox(0,"",_VersionCompareX("3.2.8", "3.2.8.0")) ;Same (return 0)

;Straight compare
MsgBox(0,"",_VersionCompareX("3.2.8.1", "3.2.8.3")) ; String 2 is greater (return -1)

;Compare against single digit
MsgBox(0,"",_VersionCompareX("5", "3.2.8.3")) ; String 1 is greater (return 1)

;Compare alphanumeric
MsgBox(0,"",_VersionCompareX("3.2.8.3", "3.2.8.3a")) ; String 2 is greater (return -1)

;Compare version numbers with extreme lengths
MsgBox(0,"",_VersionCompareX("3", "3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0")) ; Same (return 0)

;Compare version numbers with extreme lengths & alpha
MsgBox(0,"",_VersionCompareX("3", "3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.a")) ; String 2 is greater (return -1)

Func _VersionCompareX(\$sOne, \$sTwo)
;Set default return value to same
Local \$returnValue = 0

Local \$tempOne = StringSplit(\$sOne, "."), \$tempTwo = StringSplit(\$sTwo, ".")

;Determine number of positions to compare against
Local \$numPositions = \$tempOne[0]
If \$tempTwo[0] > \$tempOne[0] Then \$numPositions = \$tempTwo[0]

;Declare temp arrays
Dim \$arrayOne[\$numPositions], \$arrayTwo[\$numPositions]

;Fill temp arrays with zeros
For \$X = 0 to \$numPositions - 1
\$arrayOne[\$X] = 0
\$arrayTwo[\$X] = 0
Next

;Copy anything available from first input version
For \$X = 1 to \$tempOne[0]
If \$X > \$numPositions Then ExitLoop

\$stripped = StringRegExpReplace (\$tempOne[\$X], "^0+", "")

If \$stripped <> "" Then \$arrayOne[\$X - 1] = \$stripped
Next

;Copy anything available from second input version
For \$X = 1 to \$tempTwo[0]
If \$X > \$numPositions Then ExitLoop

\$stripped = StringRegExpReplace (\$tempTwo[\$X], "^0+", "")

If \$stripped <> "" Then \$arrayTwo[\$X - 1] = \$stripped
Next

;Compare each individual element instead of whole string
For \$X = 0 to \$numPositions - 1
Switch StringCompare (\$arrayOne[\$X],\$arrayTwo[\$X])
Case 0
\$returnValue = 0
Case -1
\$returnValue = -1
ExitLoop
Case 1
\$returnValue = 1
ExitLoop
EndSwitch
Next

Return \$returnValue
EndFunc```

Edited by weaponx, 15 October 2007 - 12:42 AM.

### #16 PsaltyDS

PsaltyDS

Most Venerable Penguin

• MVPs
• 13,279 posts

Posted 15 October 2007 - 02:49 AM

Well now that we have thoroughly hijacked this thread...

You guys needed dates for this weekend...
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

### #17 weaponx

weaponx

I'm coming for blood, no code of conduct, no law.

• MVPs
• 5,366 posts

Posted 15 October 2007 - 12:50 PM

;#include<Date.au3>

A little AutoIt humor.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users