使用VB脚本进行自然排序

前端之家收集整理的这篇文章主要介绍了使用VB脚本进行自然排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用自然排序对项目进行排序:

“Z1”,“Z3”,“Z2”,“Z20”,“Z10”

排序后,我期待以下订单:

“Z1”,“Z10”,“Z20”

当我尝试使用数组列表时,

Set oAlist=CreateObject("System.Collections.ArrayList")
oAlist.sort

我得到一个基于ASCII的排序结果:

Z1,Z10,Z2,Z20,Z3

请帮我看看如何使用vb脚本进行自然排序

理论上见 here(点击链接!).实用演示

记录:

Option Explicit

Const adInteger          =          3 ' 00000003
Const adVarChar          =        200 ' 000000C8

Dim sInp : sInp = "Z1,Z3,Z10"
Dim aInp : aInp = Split(sInp,",")
WScript.Echo "A:",Join(aInp)

Dim oRS : Set oRS = CreateObject("ADODB.Recordset")
oRS.Fields.Append "Word",adVarChar,50
oRS.Fields.Append "Length",adInteger
oRS.Open
Dim sWord
For Each sWord In aInp
    oRS.AddNew
    oRS.Fields("Word").value = Left(sWord,1)
    oRS.Fields("Length").value = CInt(Mid(sWord,2))
    oRS.UpDate
Next
oRS.Sort = "Word,Length"

Dim aTable : aTable = oRS.GetRows()
ReDim aOut(UBound(aTable,2))
Dim i
For i = 0 To UBound(aOut)
    aOut(i) = aTable(0,i) & aTable(1,i)
Next
WScript.Echo "B:",Join(aOut)

数组列表

Option Explicit

Dim sInp : sInp = "Z1,E1,D3,C2,B20,A10"
Dim aInp : aInp = Split(sInp,Join(aInp)

Dim oNAL : Set oNAL = CreateObject( "System.Collections.ArrayList" )
Dim oSB  : Set oSB  = CreateObject( "System.Text.StringBuilder" )
Dim sWord
For Each sWord In aInp
    oSB.AppendFormat_2 "{0}{1,4}",Left(sWord,1),CInt(Mid(sWord,2))
    sWord = oSB.ToString()
    oSB.Length = 0
    oNAL.Add sWord
Next
oNAL.Sort

ReDim aOut(oNAL.Count - 1)
Dim i
For i = 0 To UBound(aOut)
    aOut(i) = Left(oNAL(i),1) & CInt(Mid(oNAL(i),2))
Next
WScript.Echo "B:",Join(aOut)

猜你在找的VB相关文章