2016-06-21 26 views
2

如何使用VB腳本對字符串和數字值進行排序?使用VB腳本對數字進行排序

下面是從表的每一行我的琴絃:

  1. 「測試1次12月2日」
  2. 「測試3失敗」
  3. 「測試2通6月4日」
  4. 「驗證」
  5. 「測試十次傳」
  6. 「用戶接受」

我會喜歡得到以下順序排序(自然順序)後:

  1. 「試驗1通12月02日」
  2. 「試驗2通6月4日」
  3. 「測試3失敗」
  4. 「測試10通」
  5. 「用戶接受」
  6. 「已驗證」

途徑我有到目前爲止已經試過,

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

ArrayList是基於ASCII,我不喜歡下面順序進行排序的:

  1. 「測試1次12月2日」
  2. 「測試十次傳」
  3. 「測試2通6月4日」
  4. 「測試3失敗」
  5. 「用戶接受」
  6. 「已驗證」

我已經試過此鏈接Sort

,我不知道如何使用AppendFormat在我的情況。

注意:我給出的字符串或者是完全字符串或者帶有數字的字符串(動態),所以不知道如何在這裏使用RecordSet或AppendFormat,因爲我是編程新手。

+1

我很困惑你真的是什麼與這裏一起工作。 Systems.Collections.ArrayList是一個.Net對象...所以你使用舊的VBScript工作VB.Net類型? –

+0

[自然排序使用VB腳本]可能的重複(http://stackoverflow.com/questions/37928754/natural-sorting-using-vb-script) – Blackwood

回答

0

你可以有另一個例子。

Sub Sort 
    Set rs = CreateObject("ADODB.Recordset") 
    If LCase(Arg(1)) = "n" then 
    With rs 
     .Fields.Append "SortKey", 4 
     .Fields.Append "Txt", 201, 5000 
     .Open 
     Do Until Inp.AtEndOfStream 
      Lne = Inp.readline 
      SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3))) 
      If IsNumeric(Sortkey) = False then 
       Set RE = new Regexp 
       re.Pattern = "[^0-9\.,]" 
       re.global = true 
       re.ignorecase = true 
       Sortkey = re.replace(Sortkey, "") 
      End If 
      If IsNumeric(Sortkey) = False then 
       Sortkey = 0 
      ElseIf Sortkey = "" then 
       Sortkey = 0 
      ElseIf IsNull(Sortkey) = true then 
       Sortkey = 0 
      End If 
      .AddNew 
      .Fields("SortKey").value = CSng(SortKey) 
      .Fields("Txt").value = Lne 
      .UpDate 
     Loop 
     If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC" 
     If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC" 
     .Sort = SortColumn 
     Do While not .EOF 
      Outp.writeline .Fields("Txt").Value 
      .MoveNext 
     Loop 
    End With 

    ElseIf LCase(Arg(1)) = "d" then 
    With rs 
     .Fields.Append "SortKey", 4 
     .Fields.Append "Txt", 201, 5000 
     .Open 
     Do Until Inp.AtEndOfStream 
      Lne = Inp.readline 
      SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3))) 
      If IsDate(Sortkey) = False then 
       Set RE = new Regexp 
       re.Pattern = "[^0-9\\\-:]" 
       re.global = true 
       re.ignorecase = true 
       Sortkey = re.replace(Sortkey, "") 
      End If 
      If IsDate(Sortkey) = False then 
       Sortkey = 0 
      ElseIf Sortkey = "" then 
       Sortkey = 0 
      ElseIf IsNull(Sortkey) = true then 
       Sortkey = 0 
      End If 
      .AddNew 
      .Fields("SortKey").value = CDate(SortKey) 
      .Fields("Txt").value = Lne 
      .UpDate 
     Loop 
     If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC" 
     If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC" 
     .Sort = SortColumn 
     Do While not .EOF 
      Outp.writeline .Fields("Txt").Value 
      .MoveNext 
     Loop 
    End With 


    ElseIf LCase(Arg(1)) = "t" then 
    With rs 
     .Fields.Append "SortKey", 201, 260 
     .Fields.Append "Txt", 201, 5000 
     .Open 
     Do Until Inp.AtEndOfStream 
      Lne = Inp.readline 
      SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3))) 
      .AddNew 
      .Fields("SortKey").value = SortKey 
      .Fields("Txt").value = Lne 
      .UpDate 
     Loop 
     If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC" 
     If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC" 
     .Sort = SortColumn 
     Do While not .EOF 
      Outp.writeline .Fields("Txt").Value 
      .MoveNext 
     Loop 
    End With 
    ElseIf LCase(Arg(1)) = "tt" then 
    With rs 
     .Fields.Append "SortKey", 201, 260 
     .Fields.Append "Txt", 201, 5000 
     .Open 
     Do Until Inp.AtEndOfStream 
      Lne = Inp.readline 
      SortKey = Trim(Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))) 
      .AddNew 
      .Fields("SortKey").value = SortKey 
      .Fields("Txt").value = Lne 
      .UpDate 
     Loop 
     If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC" 
     If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC" 
     .Sort = SortColumn 
     Do While not .EOF 
      Outp.writeline .Fields("Txt").Value 
      .MoveNext 
     Loop 
    End With 
    End If 
End Sub 
0

由於您正在使用字符串,因此您將需要編寫一個自定義的排序函數,它可以解析字符串中的測試編號。

或者,您可以預處理您的列表並將數字解析到單獨的字段中,然後根據該字段進行排序。

0

要申請從here的技術問題(使用的,而不是一個RegExp Split):

Option Explicit 

Dim aInp : aInp = Array(_ 
     "Test 1 pass dec 2" _ 
    , "Test 3 fail" _ 
    , "Test 2 pass jun 4" _ 
    , "Verified" _ 
    , "Test 10 pass" _ 
    , "User Accepted" _ 
) 
WScript.Echo "----- Input:", vbCrLf & Join(aInp, vbCrLf) 
Dim aOtp : aOtp = Array(_ 
     "Test 1 pass dec 2" _ 
    , "Test 2 pass jun 4" _ 
    , "Test 3 fail" _ 
    , "Test 10 pass" _ 
    , "User Accepted" _ 
    , "Verified" _ 
) 
WScript.Echo "----- Expected:", vbCrLf & Join(aOtp, vbCrLf) 

Dim oNAL : Set oNAL = CreateObject("System.Collections.ArrayList") 
Dim oSB : Set oSB = CreateObject("System.Text.StringBuilder") 
Dim sInp, aParts, aWTF 
For Each sInp In aInp 
    aParts = Split(sInp, " ", 3) 
    Select Case UBound(aParts) 
     Case 0 ' add 2 blank elms to "verified" 
     aWTF = aParts 
     ReDim Preserve aWTF(2) 
     Case 1 ' put an empty elm in the middle 
     ' aParts = Array(aParts(0), "", aParts(1)) 
     ' ==> VBScript runtime error: This array is fixed or temporarily locked 
     aWTF = Array(aParts(0), "", aParts(1)) 
     Case 2 ' What the doctor ordered 
     aWTF = aParts 
     Case Else 
     Err.Raise "Shit hits fan" 
    End Select 
    oSB.AppendFormat_3 "{0}{1,4}{2}", aWTF(0), aWTF(1), aWTF(2) 
    sInp = oSB.ToString() & "|" & sInp ' dirty trick: append org data th ease 'reconstruction' 
    oSB.Length = 0 
    oNAL.Add sInp 
Next 
oNAL.Sort 

ReDim aOut(oNAL.Count - 1) 
Dim i 
For i = 0 To UBound(aOut) 
    aOut(i) = Split(oNAL(i), "|")(1) 
Next 
WScript.Echo "----- Output:", vbCrLf & Join(aOut, vbCrLf) 

輸出:

cscript 37946075.vbs 
----- Input: 
Test 1 pass dec 2 
Test 3 fail 
Test 2 pass jun 4 
Verified 
Test 10 pass 
User Accepted 
----- Expected: 
Test 1 pass dec 2 
Test 2 pass jun 4 
Test 3 fail 
Test 10 pass 
User Accepted 
Verified 
----- Output: 
Test 1 pass dec 2 
Test 2 pass jun 4 
Test 3 fail 
Test 10 pass 
User Accepted 
Verified 

只是爲了好玩:在 '同',但使用RegExp(更好的縮放技術):

... 
Dim r : Set r = New RegExp 
r.Pattern = "^(\w+\s*)(\d+\s*)?(.*)$" 
Dim sInp, m, aParts(2) 
Dim i 
For Each sInp In aInp 
    Set m = r.Execute(sInp) 
    If 1 = m.Count Then 
     For i = 0 To 2 
      aParts(i) = m(0).SubMatches(i) 
     Next 
    Else 
     Err.Raise "Shit hits fan" 
    End If 
    oSB.AppendFormat_3 "{0}{1,4}{2}", aParts(0), aParts(1), aParts(2) 
    sInp = oSB.ToString() & "|" & sInp ' dirty trick: append org data to ease 'reconstruction' 
... 
相關問題