2017-07-28 34 views

回答

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 

正是我想要的。

+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 
+0

對不起,這隻適用於數字或字符排序。我需要像1,1a,1b,2a,2c那樣排序...在C#中有一個簡單的解決方案。不知道是否有更好的方法用vbscript實現。 –

+0

我懷疑是否有。 –

+0

你會發現很多c +在stackoverflow本身的解決方案。 :) –