How to sort array of version number strings in VBScript?

扶醉桌前 提交于 2019-12-03 18:17:41

问题


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.

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

What I need is:

1.0.1
1.0.2
1.0.10
etc

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.

I've had a good look through related questions on here, but none are relevant to Windows or VBScript.


回答1:


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

output:

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

Get the cFormat Class from here



来源:https://stackoverflow.com/questions/10659516/how-to-sort-array-of-version-number-strings-in-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!