如何在VBScript中对版本号字符串数组进行排序? [英] How to sort array of version number strings in VBScript?

查看:109
本文介绍了如何在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()进行排序以查找最新版本以进行检出和构建。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆