Results 1 to 9 of 9

Thread: sorting an array by value

  1. #1
    bzemer
    Guest

    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?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    The least painful way would be to load your array into a ListBox and let it do the sorting.

  3. #3
    Megatron
    Guest
    Try this method. It's known as Bubble Sorting.
    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     Dim Numbers(10) As Integer
    4.    
    5.     'Add random numbers to the array
    6.     For I = 0 To 10
    7.         Randomize Timer
    8.         Numbers(I) = Int(Rnd * 100)
    9.     Next I
    10.    
    11.     'Sort the array
    12.     SortArray Numbers
    13.    
    14.     'Display the array
    15.     Text1 = ""
    16.     For I = 0 To 10
    17.         Text1 = Text1 & Numbers(I) & " "
    18.     Next I
    19. End Sub
    20.  
    21. Private Sub SortArray(ByRef ar As Variant)
    22.     For x = LBound(ar) To UBound(ar)
    23.         For y = LBound(ar) To UBound(ar)
    24.             If Val(ar(x)) < Val(ar(y)) Then Swap ar(x), ar(y)
    25.         Next y
    26.     Next x
    27. End Sub
    28.  
    29. Private Sub Swap(x As Variant, y As Variant)
    30.     Dim temp As Long
    31.     temp = y
    32.     y = x
    33.     x = temp
    34. End Sub

  4. #4
    bzemer
    Guest
    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..

  5. #5
    bzemer
    Guest
    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..

  6. #6
    bzemer
    Guest
    hello?!?!?!!?
    people?? megatron??

    i'm still hanging out here....

  7. #7
    Megatron
    Guest
    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:
    1. Private Sub Command1_Click()
    2.    
    3.     Dim Numbers(5) As String
    4.    
    5.     'Add random numbers to the array
    6.     For I = 0 To 5
    7.         Randomize Timer
    8.         Numbers(I) = Int(Rnd * 100) & " - " & I
    9.     Next I
    10.    
    11.     'Sort the array
    12.     SortArray Numbers
    13.    
    14.     'Display the array
    15.     Text1 = ""
    16.     For I = 0 To 5
    17.         Text1 = Text1 & "Party " & Right$(Numbers(I), 1) & " rolled a " & Val(Numbers(I)) & vbNewLine
    18.     Next I
    19.     Text1 = Text1 & "Therefor, Party " & Right(Numbers(LBound(Numbers)), 1) & " had the lowest."
    20.    
    21. End Sub
    22.  
    23. Private Sub SortArray(ByRef ar As Variant)
    24.     For x = LBound(ar) To UBound(ar)
    25.         For y = LBound(ar) To UBound(ar)
    26.             If Val(ar(x)) < Val(ar(y)) Then Swap ar(x), ar(y)
    27.         Next y
    28.     Next x
    29. End Sub
    30.  
    31. Private Sub Swap(x As Variant, y As Variant)
    32.     Dim temp As Variant
    33.     temp = y
    34.     y = x
    35.     x = temp
    36. End Sub

  8. #8
    Megatron
    Guest
    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.

  9. #9
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    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
  •  



Click Here to Expand Forum to Full Width