按字母顺序反向排序

时间:2017-08-07 15:06:40

标签: arrays sorting vbscript

我正在尝试使用VBScript按Z-A对文本文件(CSV文件)进行排序。 我的文件文件看起来像:

ZYUIP, ALGORITHM,TESTING,\ ,TABLE1

我想从Z-A订购CSV文件,但我被阻止将CSV文件放入数组。

这是按Z-A命令数组的代码:

ArrayOfTerms = Array("B","A","C","D")

For a = UBound(ArrayOfTerms) - 1 To 0 Step -1
    For j= 0 To a
         If ArrayOfTerms(j)<ArrayOfTerms(j+1) Then
            temp = ArrayOfTerms(j+1)
            ArrayOfTerms(j+1) = ArrayOfTerms(j)
            ArrayOfTerms(j) = temp
        End If
    Next
Next

2 个答案:

答案 0 :(得分:0)

您可以使用.Net框架中的ArrayList.Sort。必须逐个输入项目。

Dim ArrayList : Set ArrayList = CreateObject("System.Collections.ArrayList")

'Add your items here:
ArrayList.Add "B" : ArrayList.Add "A" : ArrayList.Add "C" : ArrayList.Add "D"

ArrayList.Sort
ArrayList.Reverse

答案 1 :(得分:0)

如果无法下载.Net Framework。这是我的解决方案(不是优化)将CSV文件从Z排序到A:

'---------------------------------------
'  Load a CSV File into a VBScript Array
'----------------------------------------
Function CSVArray(CSVFile)

   comma = ","
   quote = Chr(34)

   colMax  = -1

   rowCount     = -1
   Set inCsvSys = CreateObject("Scripting.FileSystemObject") 
   Set inCsv    = inCsvSys.OpenTextFile(CSVFile,"1",True)
   Do While Not inCsv.AtEndOfStream
     rowCount = rowCount + 1
     Redim Preserve inRow(rowCount)
     inRow(rowCount) = inCsv.ReadLine
   Loop
   inCsv.Close

   For r = 0 to rowCount

     csvRecord = inRow(r)
     colNum = -1
     charPos = 0
     cellComplete = True

     Do While charPos < Len(csvRecord)

       If (cellComplete = True) Then
         colNum       = colNum + 1
         cellPos      = 0
         cellQuoted   = False
         cellComplete = False
         If colNum > colMax Then
           colMax = colNum
           Redim Preserve cellArray(rowCount,colMax)
         End If              
       End If

       charPos = charPos + 1
       cellPos = cellPos + 1
       charVal = Mid(csvRecord, charPos, 1)
       If (charVal = quote) Then
         If (cellPos = 1) Then
           cellQuoted = True
           charVal    = ""
         Else
           Select Case Mid(csvRecord, charPos+1, 1)
           Case quote
             charPos = charPos + 1
           Case comma
             charPos = charPos + 1
             cellComplete = True 
           End Select
         End If
       ElseIf (charVal = comma) And (cellQuoted = False) Then
         cellComplete = True
       End If
       If (cellComplete = False) Then
         cellArray(r,colNum) = cellArray(r,colNum)&charVal
       End If

     Loop

   Next
   CSVArray = cellArray
 End Function



csv = CSVArray("\\Ad.ing.net\wps\BE\D\UD\002001\D-JS15GY\Desktop\migrate_RA\externalfiles.csv")

Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.CreateTextFile("\\Ad.ing.net\wps\BE\D\UD\002001\D-JS15GY\Desktop\migrate_RA\externalfiles2.csv",2,true)

'------------------------------
'Order the CSV File from Z to A 
'------------------------------

For a = UBound(csv,1) - 1 To 0 Step -1
   for j= 0 to a
    if csv(j,0)<csv(j+1,0) then 
    temp=csv(j+1,0)
    csv(j+1,0)=csv(j,0)
        csv(j,0)=temp

    End If  
Next 

Next 

For j = 0 to Ubound(csv,1) 

    objFile.WriteLine csv(j,0)

Next