Solved

Sorting variables in array

Posted on 1999-01-26
4
172 Views
Last Modified: 2010-05-03
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
Comment
Question by:timothy1
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 50 total points
Comment Utility
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
        List1.AddItem Str(arr(i))
    Next
End Sub

Private Sub Form_Load()
    Dim i As Integer
    For i = 0 To 6
        arr(i) = Rnd() * 100
        List1.AddItem Str(arr(i))
    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
 
LVL 12

Expert Comment

by:mark2150
Comment Utility
Cheat. Put them in a SORTED listbox and read them all out sequentially.

M

0
 

Author Comment

by:timothy1
Comment Utility
I like the cheat. it's simplier.
0
 
LVL 13

Expert Comment

by:Mirkwood
Comment Utility
Yes, Ok in the example I use a listbox. But maybe he wants to just have a sorted array in memory.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now