• Status: Solved
• Priority: Medium
• Security: Public
• Views: 220

# Sorting variables in array

Okay, I have an array filled with six randomly chosen integers:
numb(1 to 6)
How do I sort them so that numb(1) is lowest and numb(6) is highest?
For Example:
15, 19, 25, 28, 47, 66
0
timothy1
• 2
1 Solution

Commented:
Sure here is a sample

Option Explicit
Dim arr(6) As Integer

Private Sub Command1_Click()
sort arr
List1.Clear
Dim i As Integer
For i = 0 To 6
Next
End Sub

Dim i As Integer
For i = 0 To 6
arr(i) = Rnd() * 100
Next
End Sub

Private Sub sort(ByRef arr As Variant)
Dim i As Integer
Dim j As Integer
Dim t As Variant

For i = LBound(arr) To UBound(arr)
For j = LBound(arr) To UBound(arr) - 1
If (arr(j) > arr(j + 1)) Then
t = arr(j)
arr(j) = arr(j + 1)
arr(j + 1) = t
End If
Next j
Next i
End Sub

0

Commented:
Cheat. Put them in a SORTED listbox and read them all out sequentially.

M

0

Author Commented:
I like the cheat. it's simplier.
0

Commented:
Yes, Ok in the example I use a listbox. But maybe he wants to just have a sorted array in memory.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.