Solved

Sorting variables in array

Posted on 1999-01-26
4
185 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
ID: 1470726
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
ID: 1470727
Cheat. Put them in a SORTED listbox and read them all out sequentially.

M

0
 

Author Comment

by:timothy1
ID: 1470728
I like the cheat. it's simplier.
0
 
LVL 13

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

896 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

17 Experts available now in Live!

Get 1:1 Help Now