i would like to Sort an array in vbs, so the numbers which are contained in the array are ascending. I'm reading the numbers from a text file
Sub Main()
Dim Werte(10)
Dim c
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set c = fso.Drives
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("Y:\Benutzer\Desktop\Werte.txt",1)
Dim strLine
Dim i
i=0
do while not objFileToRead.AtEndOfStream
strLine = objFileToRead.ReadLine()
Werte(i)=strLine
i=i+1
loop
objFileToRead.Close
Set objFileToRead = Nothing
for x=LBound(Werte) To UBound(Werte) - 1 Step 1
for j= 0 to UBound(Werte)-1
if Werte(j)>Werte(j+1) then
temp=Werte(j+1)
Werte(j+1)=Werte(j)
Werte(j)=temp
end if
next
next
PATH="Y:\Benutzer\Desktop\Werte_sortiert.txt"
Array_ToTextFile Werte,PATH
End Sub
Function Array_ToTextFile(a,path)
Const ForWriting = 2
Dim fso
Dim writer
Dim i
Set fso = CreateObject("Scripting.FileSystemObject")
Set writer = fso.OpenTextFile(path,ForWriting,True)
For i = lbound(a) to ubound(a)
writer.writeline a(i)
Next
writer.close
End Function
Main
Text file looks like this:
10 9 8 7 6 5 4 3 2 1 0
Result looks like this:
0 1 10 2 3 4 5 6 7 8 9
The number 10 should be at the end, but is at the 3rd position after sorting. I would appreciate your help.