Friday, November 19, 2010

VBScript Code - Sorting an array

Write the below code in a notepad and save it as a .vbs file. Either run it from command prompt or run it by double-clicking on it.

VBScript Code - Sorting an array

Function MySort(arr_ind)

for i = UBound(arr_ind) - 1 To 0 Step -1
 for j= 0 to i
  if arr_ind(j)>arr_ind(j+1) then
   temp=arr_ind(j+1)
   arr_ind(j+1)=arr_ind(j)
   arr_ind(j)=temp
  end if
 next
next

MySort = arr_ind

End function

'Declared an Array
Dim Arr(4)
arr(0)=3
arr(1)=2
arr(2)=5
arr(3)=0
arr(4)=1

'Before Sorting
For i=0 to ubound (Arr)
 b=b &" " & Arr(i)
'msgbox Arr(i)
Next
msgbox b

'Calling Sorting function
XX=MySort(Arr)

'After Sorting
For i=0 to ubound (XX)
 a=a &" " & XX(i)
'msgbox XX(i)
Next
msgbox a