如何在 VBScript 中对版本号字符串数组进行排序? [英] How to sort array of version number strings in VBScript?
问题描述
我使用 SVN 和标签来标记版本,每个标签目录都以版本号命名.这一切都很好,但现在我正在使用 Visual Build Pro 自动化这个构建过程.
I am using SVN and tags to label releases, with each tag directory named with the version number. This is all fine, but now I am in the process of automating this build process using Visual Build Pro.
我使用 svn ls
来检索标签列表(当然它不会按排序顺序返回),我使用它然后使用 Split 放入数组()
进行排序以找到最新的检查和构建.这一切都是在 VBScript 中进行的.
I am using svn ls
to retrieve a list of tags (which of course it doesn't return in a sorted order), which I use and then put into an array using Split()
to sort to find the latest one to check out and build. This is all carried out in VBScript.
问题在于,作为字符串,版本号不是按数字排序,而是按字母顺序排序.给你:
The trouble is that as strings, the version numbers don't sort numerically, but alphabetically. Giving you:
1.0.1
1.0.10
1.0.2
etc
我需要的是:
1.0.1
1.0.2
1.0.10
etc
在 .NET 中很容易,因为我可以创建一个 List(Of Version)
并对其进行排序,但我不能在 VBScript 中这样做.我可以使用 Set verObj = Create.Object("System.Version")
创建一个 Version 类型,但这本身就不好.
In .NET it is easy as I could just create a List(Of Version)
and sort that, but I can't do that in VBScript. I can create a Version type using Set verObj = Create.Object("System.Version")
, but that's no good on it's own.
我已经仔细浏览了此处的相关问题,但没有一个与 Windows 或 VBScript 相关.
I've had a good look through related questions on here, but none are relevant to Windows or VBScript.
推荐答案
由于 VBScript 没有本机排序,您必须使用其他技术:具有合适排序功能的 Javascript/JScript、断开连接的 ADO 记录集或 .NET ArrayList 具有一些创造性的格式.演示最后一个选项:
As VBScript has no native sort, you'll have to use other techniques: Javascript/JScript with a suitable sort function, a disconnected ADO recordset, or a .NET ArrayList with a bit of creative formatting. To demonstrate the last option:
Dim aTests : aTests = Array( _
"1.0.1" _
, "1.0.10" _
, "1.0.2" _
)
WScript.Echo Join(aTests, vbCrLf)
Dim oFmt : Set oFmt = New cFormat
Dim alVers : Set alVers = CreateObject("System.Collections.ArrayList")
Dim sVers
For Each sVers In aTests
Dim aParts : aParts = Split(sVers, ".")
alVers.Add oFmt.formatArray("{0,4}.{1,4}.{2,4}", aParts)
Next
alVers.Sort
WScript.Echo "---------------"
WScript.Echo Join(alVers.ToArray(), vbCrLf)
WScript.Echo "---------------"
For Each sVers In alVers
WScript.Echo Replace(sVers, " ", "")
Next
输出:
1.0.1
1.0.10
1.0.2
---------------
1. 0. 1
1. 0. 2
1. 0. 10
---------------
1.0.1
1.0.2
1.0.10
从此处
这篇关于如何在 VBScript 中对版本号字符串数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!