-
Can anyone help me with this problem?
I have to sort the random numbers in lstVelg in lstSorter
by using selection sort.
Thank you!
Option Explicit
Option Base 1 'setter nedre grense til 1
Private Sub cmdExit_Click()
End 'avslutter program
End Sub
Private Sub cmdSorter_Click()
End Sub
Private Sub cmdVelg_Click()
Dim teller As Integer
Dim Tall(10) As Integer
lstVelg.Clear 'tømmer lista for innhold
lstSorter.Clear 'tømmer lista for innhold
'velger 10 tall
For teller = 1 To 10 'teller is counter
Randomize
Tall(teller) = 1 + Int(1000 * Rnd())'tall is number
lstVelg.AddItem Tall(teller)
Next teller
End Sub
-
<?>
Code:
Option Explicit
Option Base 1 'setter nedre grense til 1
Sub SortNum(iArray As Variant)
Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lTemp As Long
For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
For lLoop2 = LBound(iArray) + 1 To lLoop1
If iArray(lLoop2 - 1) > iArray(lLoop2) Then
lTemp = iArray(lLoop2 - 1)
iArray(lLoop2 - 1) = iArray(lLoop2)
iArray(lLoop2) = lTemp
End If
Next lLoop2
Next lLoop1
End Sub
Private Sub cmdExit_Click()
Unload Me
'End 'avslutter program
End Sub
Private Sub cmdSorter_Click()
End Sub
Private Sub cmdVelg_Click()
Dim teller As Integer
Dim Tall(10) As Integer
Dim myArr() 'array for information
lstVelg.Clear 'tømmer lista for innhold
lstSorter.Clear 'tømmer lista for innhold
'velger 10 tall
For teller = 1 To 10 'teller is counter
ReDim Preserve myArr(teller)
Randomize
Tall(teller) = (1 + Int(1000 * Rnd())) 'tall is number
lstVelg.AddItem Tall(teller)
myArr(teller) = Tall(teller)
Next teller
'sort the array and load it in lstSorter
Call SortNum(myArr)
For teller = LBound(myArr) To UBound(myArr)
lstSorter.AddItem myArr(teller)
Next teller
End Sub