如何使用VBScript对数组进行排序(" 10a"," 23a"," 1a"," 2a")

时间:2017-07-28 13:08:57

标签: sorting vbscript

如何在数组下面排序

Dim testarray = new Array("10a", "23a", "2c", "2a")

使用VBScript提供类似"2a", "2c","10a", "23a"的输出。

2 个答案:

答案 0 :(得分:0)

您可以使用ArrayList代替数组:

testarrray = Array("10a", "23a", "2c", "2a")

Set testlist = CreateObject("System.Collections.ArrayList")
For i=0 To UBound(testarray)
    testlist.Add testarray(i)
Next

For Each e In testlist
    WScript.Echo e
Next

testarray.Sort

For Each e In testlist
    WScript.Echo e
Next

否则您必须自己实施sorting algorithm。 VBScript没有内置的功能。

对于像你的例子一样的简单情况,可能最容易实现的是:

Function Bubblesort(ByVal arr)
    For i = 0 To UBound(arr)
        For j = i + 1 to UBound(arr)
            If arr(i) > arr(j) Then
                tmp    = arr(i)
                arr(i) = arr(j)
                arr(j) = tmp
            End If
        Next
    Next
    Bubblesort = arr
End Function

答案 1 :(得分:0)

我找到的唯一解决方法是拆分数字部分,然后先比较数字部分,然后比较字符部分。

Function sort(iparrary)
For i = 0 to ubound(iparrary)
        For j= i+1 to ubound(iparrary)
            num1 = getnumber(iparrary(i))
            num2 = getnumber(iparrary(j))

            if (num1 <> "" and num2 <> "") then
                if cint(num1) > cint(num2) then
                    temp  = iparrary(i)
                    iparrary(i) = iparrary(j)
                    iparrary(j) = temp
                End if  
            End if

            if num1 = num2 or num1="" or num2 = "" then
                if(iparrary(i) > iparrary(j)) then
                    temp  = iparrary(i)
                    iparrary(i) = iparrary(j)
                    iparrary(j) = temp  
                End if
            End if      
        Next    
    Next
End Function

Function getnumber(strnumber)

    dim intnum
    intnum  = ""

    For num = 1 to len(strnumber)
        chardata = mid(strnumber,num,1)
        if isnumeric(chardata) then
            intnum= intnum & chardata
        else
            getnumber = intnum
            Exit function
        End if      
    Next
    getnumber = intnum
End Function

正是我想要的。