PDA

View Full Version : How to sort numbers in asceding order?



AlexAlexon
11-17-2007, 02:45 AM
This is Lottery program which generate random numbers, but output is not sorted. How to sort numbers in asceding order?
This is VBA Express.

Public Class ltaForm

Private Sub GetNumbersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetNumbersToolStripMenuItem.Click
Dim intForLoopCtr As Integer = 0
Dim blnFullSetComplete As Boolean = False
Dim intRndNo As Integer = 0
Dim strDisplayString As String = ""
Dim intNoOfValidPics As Integer = 0
Dim aintLotteryArray(10) As Array
Dim intNumberCount As Integer = 0
Dim strTestString As String = "_"
If txtFullSet.Text = "" Then
MessageBox.Show("You must specify how many numbers " & _
"make up a full set.")
Return
End If
If IsNumeric(txtFullSet.Text) = False Then
MessageBox.Show("You must specify numeric input when " & _
"specifying how many numbers make up a full set.")
Return
End If
For intForLoopCtr = 1 To CInt(txtNoPics.Text)
Do Until blnFullSetComplete = True
Randomize()
intRndNo = _
FormatNumber(Int((txtNoRange.Text * Rnd()) + 1))
If InStr(strTestString, _
Convert.ToString("_" & intRndNo & "_")) = 0 Then
strDisplayString = strDisplayString & " " & _
intRndNo & ControlChars.Tab
intNoOfValidPics = intNoOfValidPics + 1
strTestString = strTestString & intRndNo & "_"
End If
If intNoOfValidPics = Int32.Parse(txtFullSet.Text) Then
blnFullSetComplete = True
strDisplayString = strDisplayString & _
ControlChars.NewLine & ControlChars.NewLine
strTestString = "_"


End If

Loop
blnFullSetComplete = False
intNoOfValidPics = 0
Next
txtOutput.Text = strDisplayString
GetNumbersToolStripMenuItem.Enabled = False
ClearToolStripMenuItem.Enabled = True






End Sub

Aussiebear
11-17-2007, 02:49 AM
What does the macro recorder suggest?

AlexAlexon
11-17-2007, 03:11 AM
This is vbaexpress not excel.

Aussiebear
11-17-2007, 01:13 PM
You are absolutely correct..... so i'll ask you again. What did the macro recorder suggest? If you use the recorder then look at the code it may well give you a hint as to how you might proceed with the code.

AlexAlexon
11-17-2007, 01:56 PM
No I don't use macro recorder. This code is from book "Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner".

Code work fine and generate random numbers, but numbres are not sorted.

mikerickson
11-17-2007, 03:12 PM
It's not clear to me what the output of your routine is. Is your routine returning an array or a delimited string?

This is a bubble sort routine for sorting an array.


For i = 1 to UBound(myArray) - 1
For j = i+1 to UBound(myArray)
If myArray(j) < myArray(i) Then
tempVal = myArray(i)
myArray(i) = myArray(j)
myArray(j) = tempVal
End If
Next j
Next i

TonyJollans
11-17-2007, 03:52 PM
Your code is Visual Basic, not Visual Basic for Applications. VB is an application that uses VBA; Excel is a different application that uses VBA. Which do you want to use?

AlexAlexon
11-18-2007, 01:28 AM
Maybe this is wrong forum. I have Microsoft Visual Basic 2005 express edition, which can run this program.
If this is wrong forum please just delete thread!

TonyJollans
11-18-2007, 04:32 AM
As your question has nothing to do with Excel it is really in the wrong place but I will try to help.

Your code generates several (txtNoPics.Text) sequences of (random) numbers. You build up a string (strDisplayString) of the sequences, delimited by newline characters, as you go along.

Each individual set of numbers is also built into a second string (strTestString) which is only used for deduplication and is discarded after each sequence is finished.

You need to move the building of the Display String outside the inner loop so that you can sort each generated sequence before you add it to the display string, something along these lines ..


For intForLoopCtr = 1 To CInt(txtNoPics.Text)
Do Until blnFullSetComplete = True
Randomize()
intRndNo = _
FormatNumber(Int((txtNoRange.Text * Rnd()) + 1))
If InStr(strTestString, _
Convert.ToString("_" & intRndNo & "_")) = 0 Then
' Next line removed from this loop
' strDisplayString = strDisplayString & " " & _
' intRndNo & ControlChars.Tab
intNoOfValidPics = intNoOfValidPics + 1
strTestString = strTestString & intRndNo & "_"
End If
If intNoOfValidPics = Int32.Parse(txtFullSet.Text) Then
blnFullSetComplete = True
' Now to sort the generated sequence
' First put in an array
myArray = Split(Trim(Replace(strTestString, "_", " ")))
' then use mikerickson's bubble sort (adjusted to start at zero)
For i = LBound(myArray) To UBound(myArray) - 1
For j = i + 1 To UBound(myArray)
If myArray(j) < myArray(i) Then
tempVal = myArray(i)
myArray(i) = myArray(j)
myArray(j) = tempVal
End If
Next j
Next i
' Now add the sorted list to the display string
strDisplayString = strDisplayString & " " & _
Join(myArray, ControlChars.Tab) & ControlChars.NewLine
' all replacing what you had before
'strDisplayString = strDisplayString & _
'ControlChars.NewLine & ControlChars.NewLine
strTestString = "_"


End If

Loop

blnFullSetComplete = False
intNoOfValidPics = 0
Next





My apologies if I've got some of the VB (as opposed to VBA) bits wrong :)

TonyJollans
11-18-2007, 08:35 AM
A slight correction ..

The results of Split are string values so you should change ..
If myArray(j) < myArray(i) Then
to ..
If CInt(myArray(j)) < cint(myArray(i)) Then

AlexAlexon
11-18-2007, 08:51 AM
It works!
Thanks Tony Jollans for help and explanation!
So this problem is solved!:hi:

TonyJollans
11-18-2007, 09:54 AM
My pleasure :)