Sorting String with Numbers using VB Script

后端 未结 3 779
再見小時候
再見小時候 2021-01-07 02:59

How to Sort String with Numeric values using VB Script?

Below are my strings from each row from a table:

  1. \"Test 1 pass dec 2\"
  2. \"Test 3 fail\"
3条回答
  •  逝去的感伤
    2021-01-07 03:01

    You can have another example.

    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
    

提交回复
热议问题