Sorting 如何使用VBScript对数组(“10a”、“23a”、“1a”、“2a”)进行排序
如何在数组下面排序Sorting 如何使用VBScript对数组(“10a”、“23a”、“1a”、“2a”)进行排序,sorting,vbscript,Sorting,Vbscript,如何在数组下面排序 Dim testarray = new Array("10a", "23a", "2c", "2a") 使用VBScript提供输出,如“2a”、“2c”、“10a”、“23a”您可以使用数组而不是数组: testarrray = Array("10a", "23a", "2c", "2a") Set testlist = CreateObject("System.Collections.ArrayList") For i=0 To UBound(testarray)
Dim testarray = new Array("10a", "23a", "2c", "2a")
使用VBScript提供输出,如“2a”、“2c”、“10a”、“23a”
您可以使用数组而不是数组:
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
否则,您将不得不自己实现一个。VBScript中没有内置该功能
对于像您的示例这样的简单情况,bubblesort可能是最容易实现的:
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
我找到的唯一解决方案是分割数字部分,然后首先比较数字部分,然后比较字符部分
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
函数排序(iParray)
对于i=0到ubound(iParray)
对于j=i+1至ubound(iParray)
num1=getnumber(iParray(i))
num2=getnumber(iParray(j))
如果(num1“”和num2“”),则
如果cint(num1)>cint(num2),则
温度=i阵列(i)
iParray(i)=iParray(j)
i阵列(j)=温度
如果结束
如果结束
如果num1=num2或num1=“”或num2=“”,则
如果(iParray(i)>iParray(j)),则
温度=i阵列(i)
iParray(i)=iParray(j)
i阵列(j)=温度
如果结束
如果结束
下一个
下一个
端函数
函数getnumber(strnumber)
暗整数
intnum=“”
对于num=1到len(strnumber)
chardata=mid(strnumber,num,1)
如果是数字(chardata),则
intnum=intnum&chardata
其他的
getnumber=intnum
退出功能
如果结束
下一个
getnumber=intnum
端函数
完全符合我的要求。抱歉,这仅适用于数字或字符排序。我需要排序像1,1a,1b,2a,2c…有一个简单的解决方案。C#。不确定是否有更好的vbscript实现方法。我怀疑是否有。在stackoverflow自身中,您会发现许多使用c#的解决方案。:)我怀疑有没有更好的方法用VBScript实现这一点。这基本上就是我建议的,只是用一个自定义的比较器。这是我无法设计的,因为您选择不披露项目的预定顺序。