|
-
Dec 21st, 2001, 01:23 PM
#1
sorting an array by value
hello,
i have an array:
array1(x)
it holds up to 10 numeric values.
i want to be able to sort them from the lowest to the highest on my text box.
e.g.
array1(1)=6
array1(2)=9
aray1(3)=2
array1(4)=5
text1.text:
2
5
6
9
--------------------------------
how do i do that?
-
Dec 21st, 2001, 01:30 PM
#2
The least painful way would be to load your array into a ListBox and let it do the sorting.
-
Dec 21st, 2001, 03:55 PM
#3
Try this method. It's known as Bubble Sorting.
VB Code:
Private Sub Command1_Click()
Dim Numbers(10) As Integer
'Add random numbers to the array
For I = 0 To 10
Randomize Timer
Numbers(I) = Int(Rnd * 100)
Next I
'Sort the array
SortArray Numbers
'Display the array
Text1 = ""
For I = 0 To 10
Text1 = Text1 & Numbers(I) & " "
Next I
End Sub
Private Sub SortArray(ByRef ar As Variant)
For x = LBound(ar) To UBound(ar)
For y = LBound(ar) To UBound(ar)
If Val(ar(x)) < Val(ar(y)) Then Swap ar(x), ar(y)
Next y
Next x
End Sub
Private Sub Swap(x As Variant, y As Variant)
Dim temp As Long
temp = y
y = x
x = temp
End Sub
-
Dec 21st, 2001, 08:57 PM
#4
thanks,
but,
how can i also completly remove zero integer from the array?
if my array contained then numbers:
array(1)=5
array(2)=8
array(3)=0
array(4)=2
array(5)=6
array(6)=0
array(7)=0
array(8)=0
array(9)=0
array(10)=0
then how can i make the array delete the entire value as if the array was set to :
array(4) - 4 is because there are 4 none zero integers..
-
Dec 21st, 2001, 09:17 PM
#5
no, forget that.
what if i have the following code;
Dim RollVal As Integer
Dim R As Integer
Dim Party(10) As Integer
For R = 1 To TxtParties.Text
RollVal = Int((Rnd * 10) + 1)
Party(R) = RollVal
Party2(R, R) = RollVal
TxtResult.Text = TxtResult.Text & "Party " & R & " [rolled " & Party(R) & "]" & vbNewLine
Next R
TxtResult.Text = TxtResult.Text & "Order of parties:" & vbNewLine
SortArray Party
For R = 1 To 10
If Not Party(R) = 0 Then TxtResult.Text = TxtResult.Text & "Party " & Party(R) & " - " & Party(R) & vbNewLine
Next R
what i'm trying to do here is write in the textbox the party number and the roll of that number.
this is the only way i can explain it:
i have 6 parties:
array(1)=8
array(2)=0
array(3)=4
array(4)=0
array(5)=2
array(6)=0
i then want to sort the aray:
array(2)=0
array(4)=0
array(6)=0
array(5)=2
array(3)=4
array(1)=8
then remove the zero and then write:
array5=2
array3=4
array1=8
or in other words:
party 5 rolled 2
party 3 rolled 4
party 1 rolled 8
my problem is that i can get it to rembeber which party rolled what.
i hope you get what i mean, it's very late and i'm falling asleep as i'm typing.
oh, i also like to know in the end which party hit the lowest none zero integer.
(party 5 in the example) but how can itell and put it in the txtresult..?
thanks..
-
Dec 22nd, 2001, 07:46 AM
#6
hello?!?!?!!?
people?? megatron??
i'm still hanging out here....
-
Dec 22nd, 2001, 03:46 PM
#7
You could make the array a string, and separate the index and value with a hypen, e.g: Array(3) = "1 - 3" This shows that the element 3 has a value of one.
A bit ugly, but it works.
VB Code:
Private Sub Command1_Click()
Dim Numbers(5) As String
'Add random numbers to the array
For I = 0 To 5
Randomize Timer
Numbers(I) = Int(Rnd * 100) & " - " & I
Next I
'Sort the array
SortArray Numbers
'Display the array
Text1 = ""
For I = 0 To 5
Text1 = Text1 & "Party " & Right$(Numbers(I), 1) & " rolled a " & Val(Numbers(I)) & vbNewLine
Next I
Text1 = Text1 & "Therefor, Party " & Right(Numbers(LBound(Numbers)), 1) & " had the lowest."
End Sub
Private Sub SortArray(ByRef ar As Variant)
For x = LBound(ar) To UBound(ar)
For y = LBound(ar) To UBound(ar)
If Val(ar(x)) < Val(ar(y)) Then Swap ar(x), ar(y)
Next y
Next x
End Sub
Private Sub Swap(x As Variant, y As Variant)
Dim temp As Variant
temp = y
y = x
x = temp
End Sub
-
Dec 22nd, 2001, 07:24 PM
#8
Your welcome, bzemer.
In my opinion, the best way to learn is to practice, and keep practicing. Think of it as something you can never get enough of. A great way to get practice it to try to solve a problem on your own first. If you manage to solve it, you'll never get stuck on it again, and you would have gained some extra knowledge too. This knowledge might be irrelevant to the task at hand, yet it could be invaluable in the future. When you're totally suck, ask your question on these forums. When you get a satisfying response, analyize it. Ask yourself: What does it do? What's the logic behind it? What was I doing wrong? etc.
Another great way to practice is to answer questions on these forums. Make it your goal to answer at least three questions per day.
-
Dec 22nd, 2001, 08:13 PM
#9
Frenzied Member
Best way to learn is to practise. And hang out here with all the other smart people.
Reading a few books, and taking a VB course might help too.
~Peter

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|