Solved

Sorting variables in array

Posted on 1999-01-26
4
191 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

785 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