Results 1 to 38 of 38

Thread: [RESOLVED] sorting arrays

  1. #1

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Resolved [RESOLVED] sorting arrays

    I have an array called iarray(), whihc contains the members iarray(0) to array(4). is there a way of sorting out the higher (in terms of the array) array to the lower?

    i.e. The arrays are randomly placed in the following order:
    iarray(3)
    iarray(2)
    iarray(4)
    iarray(1)
    iarray(0)

    I would like the code to sort it so it goes from iarray(4) to iarray(0),and then put the indexes (EDIT: indicies) into a new array called order()

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    A better explaination is in order. Do you mean in number sequence? (4)(3)... or the contents of the array?

  3. #3

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    in reverse sequence (number of the array)

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    VB Code:
    1. For i = Ubound(isarray) to LBound(isarray) Step -1
    2.    Order(j) = isarray(i)
    3.    j = j + 1
    4. Next
    Last edited by randem; Nov 29th, 2005 at 12:38 PM.

  5. #5

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    how can I ensure that the random values in iarray() have been sorted into descending order? I've tried putting a msgbox in the code (just before j=j+1) but a message just pops up saying "0".

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: sorting arrays

    Here is code that sorts arrays: havent tried it with numbers but it should work..

    Make a copy of the random array.. sort the copy..
    then compare Sorted to original.. creating an array with the index order?
    sounds like it might work... but, there is probably a better way lol
    sort code:

    VB Code:
    1. 'Author: CVMichael
    2. 'Thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
    3.  
    4. Private Sub QuickSort(C() As String, ByVal First As Long, ByVal Last As Long)
    5.     Dim Low As Long, High As Long
    6.     Dim MidValue As String
    7.    
    8.     Low = First
    9.     High = Last
    10.     MidValue = C((First + Last) \ 2)
    11.    
    12.     Do
    13.     While C(Low) < MidValue
    14.     Low = Low + 1
    15.     Wend
    16.    
    17.     While C(High) > MidValue
    18.     High = High - 1
    19.     Wend
    20.    
    21.     If Low <= High Then
    22.         Swap C(Low), C(High)
    23.         Low = Low + 1
    24.         High = High - 1
    25.     End If
    26. Loop While Low <= High
    27.  
    28. If First < High Then QuickSort C, First, High
    29. If Low < Last Then QuickSort C, Low, Last
    30. End Sub
    31.  
    32. Private Sub Swap(ByRef A As String, ByRef B As String)
    33.     Dim T As String
    34.    
    35.     T = A
    36.     A = B
    37.     B = T
    38. End Sub
    Last edited by Static; Nov 29th, 2005 at 01:11 PM.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    I didnt understand that. I couldnt figure out where any of the variables went, and I couldnt ensure that any of it worked. Thanks anyway. Could someone try to work on randem's code so it will msgbox the contents of the "order" array?

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: sorting arrays

    Quote Originally Posted by ajames
    I didnt understand that. I couldnt figure out where any of the variables went, and I couldnt ensure that any of it worked.
    The original thread for that code is here:
    VB - Quick Sort algorithm (very fast sorting algorithm)
    You will understand it better if you look at the thread.

    You have to call the QuickSort like this:
    QuickSort MyArray, LBound(MyArray), UBound(MyArray)

    But the QuickSort function is made for strings, so to make it work for numbers, just replace the "As String" to "As Integer" everywhere in the code.

  9. #9
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: sorting arrays

    CVMichael: Cool.. I had forgotten where I got that code its GREAT!
    (Added Comment in my codebank snippet stating u are the author)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    CVMichael: Does that code sort the contents of the array of the indexes of the arrays?

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: sorting arrays

    Quote Originally Posted by ajames
    CVMichael: Does that code sort the contents of the array of the indexes of the arrays?
    Well, it sorts the contents of the array.

    But... you can't really sort the index of the array, not with a regular array anyways...
    An index is always sequention (in order), if you re-order the index, you loose the whole purpose of an index...

    But I think I understand what you need.
    You probably want to do something like an array of pointers (equivalent in C/C++)
    To do that in VB you need an extra array to store the position of the item in the other array, Is that what you want ?

    I think I can modify the QuickSort function to do that, but first I want to know if what I previously said is right ?

  12. #12
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: sorting arrays

    Actually, re-reading your first post, I see that that's what you want...

    I modified the code, is it right ?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type tQuickSort
    4.     Index As Long
    5.     Value As Integer
    6. End Type
    7.  
    8. Private Sub Form_Load()
    9.     Dim MyArray(10) As Integer, ArrOrder() As Long, K As Long
    10.    
    11.     MyArray(0) = 23
    12.     MyArray(1) = 345
    13.     MyArray(2) = 2312
    14.     MyArray(3) = 2
    15.     MyArray(4) = 25
    16.     MyArray(5) = 66
    17.     MyArray(6) = 77
    18.     MyArray(7) = 99
    19.     MyArray(8) = 55
    20.     MyArray(9) = 345
    21.     MyArray(10) = 232
    22.    
    23.     QuickSort MyArray, ArrOrder
    24.    
    25.     Debug.Print " Index", " Order", " Value"
    26.     For K = LBound(MyArray) To UBound(MyArray)
    27.         Debug.Print K, ArrOrder(K), MyArray(ArrOrder(K))
    28.     Next K
    29. End Sub
    30.  
    31. Private Sub QuickSort(iArray() As Integer, Order() As Long)
    32.     Dim K As Long
    33.     Dim tArray() As tQuickSort
    34.    
    35.     ReDim tArray(LBound(iArray) To UBound(iArray))
    36.    
    37.     For K = LBound(iArray) To UBound(iArray)
    38.         tArray(K).Index = K
    39.         tArray(K).Value = iArray(K)
    40.     Next K
    41.    
    42.     QuickSortRecurse tArray, LBound(iArray), UBound(iArray)
    43.    
    44.     ReDim Order(LBound(iArray) To UBound(iArray))
    45.    
    46.     For K = LBound(tArray) To UBound(tArray)
    47.         Order(K) = tArray(K).Index
    48.     Next K
    49. End Sub
    50.  
    51. Private Sub QuickSortRecurse(C() As tQuickSort, ByVal First As Long, ByVal Last As Long)
    52.     '
    53.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    54.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
    55.     '
    56.     Dim Low As Long, High As Long
    57.     Dim MidValue As tQuickSort
    58.    
    59.     Low = First
    60.     High = Last
    61.     MidValue = C((First + Last) \ 2)
    62.    
    63.     Do
    64.         While C(Low).Value < MidValue.Value
    65.             Low = Low + 1
    66.         Wend
    67.        
    68.         While C(High).Value > MidValue.Value
    69.             High = High - 1
    70.         Wend
    71.        
    72.         If Low <= High Then
    73.             If Low <> High Then Swap C(Low), C(High)
    74.             Low = Low + 1
    75.             High = High - 1
    76.         End If
    77.     Loop While Low <= High
    78.    
    79.     If First < High Then QuickSortRecurse C, First, High
    80.     If Low < Last Then QuickSortRecurse C, Low, Last
    81. End Sub
    82.  
    83. Private Sub Swap(ByRef A As tQuickSort, ByRef B As tQuickSort)
    84.     Dim T As tQuickSort
    85.    
    86.     T = A
    87.     A = B
    88.     B = T
    89. End Sub
    Last edited by CVMichael; Nov 30th, 2005 at 12:58 PM.

  13. #13
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    Isn't this a bit over-complicated???? What was wrong with post #4????

  14. #14
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: sorting arrays

    I don't understand how that works.... where is the sorting part ?

    Can you give an example with that code ?

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: sorting arrays

    The value of the array doesn't matter. #4 just sorts the order of the array.

  16. #16
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    Quote Originally Posted by ajames
    in reverse sequence (number of the array)
    Well, it was not ask for it to be sorted by contents just by the index numbers in reverse...

  17. #17
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: sorting arrays

    I'm still confuzed...

    Maybe if I see working code (example), I'll understand.

  18. #18
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    It is a working example. Re-read the first post carefully...

  19. #19
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: sorting arrays

    Quote Originally Posted by ajames
    I would like the code to sort it so it goes from iarray(4) to iarray(0),and then put the indexes (EDIT: indicies) into a new array called order()
    I don't know... I re-read it and re-read it... and I still get to the same conclusion...

    He wants to sort the array, and put a indexes in a new array.

    If it's the way you did it, then It does not make sence to put the index in a new array, why can't you just read the array in reverse order like this:

    VB Code:
    1. For I = UBound(iarray) to LBound(iarray) Step -1
    2.      Debug.Print iarray(I)
    3. Next I
    I'll wait for ajames respose to have a more clear explanation (hopefully)

    Edit
    I just realized that I did not make my code sort in reverse order, but that can be easily fixed if you just read the sorted array in reverse order like in the code above.
    Last edited by CVMichael; Nov 30th, 2005 at 04:30 PM.

  20. #20
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    well, Sorting from iarray(4) to iarray(0) should be self explanitory. It's just a reorder in reverse (as defined).

  21. #21
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: sorting arrays

    then why dont u try my example of sorting? http://www.vbforums.com/showthread.php?t=373225

  22. #22
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    Why should I???

  23. #23
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: sorting arrays

    sorry, my post was directed towards ajames.

  24. #24

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    ok, i seem to have made a difference to my code. Which was the one to sort out the contents of the arrays again?

    EDIT: This is how far I have got:

    the array 'order(4)' recieves a random number from 0 to 4 each time a button is pressed

    it then makes 'iarray(4)' show the image corresponding to its random number (0 = 10, 2 is queen, 4 = ace etc.)

    I want to be able to press a button, and get the array 'order' to replace the values so the highest number is in 'order(4)' and the lowest in 'order(0)'.
    eg:

    Contents of array 'order()':
    order(0)=3
    order(1)=1
    order(2)=4
    order(3)=0
    order(4)=2

    I would then like the code to reorganize it so:

    order(0)=4
    order(1)=3
    order(2)=2
    order(3)=1
    order(4)=0

    eg2) contents of order()
    order(0)=4
    order(1)=4
    order(2)=4
    order(3)=1
    order(4)=4

    sorted out:

    order(0)=4
    order(1)=4
    order(2)=4
    order(3)=4
    order(4)=1

    This should hopefully make sense
    Last edited by ajames; Dec 1st, 2005 at 12:49 PM.

  25. #25
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    Now, that is TOTALLY different that what you originally asked.

    So, just sort the contents of the array then step through the sorted array and move each element to a new array.

    Is the contents of the array a string, number etc???

  26. #26
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    If they are all strings, you can just place all your info into a listbox (with the sorted property set to True), then Step through the ListBox and place the data elements back into the array.

  27. #27

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    Quote Originally Posted by randem
    Now, that is TOTALLYSo, just sort the contents of the array then step through the sorted array and move each element to a new array.

    Is the contents of the array a string, number etc???
    the top bit is what i'm having trouble doing. How do I sort them?

    The contents of the array are numbers

    (i do realise that this is completly different to what i originally asked, but i figured out a new way of doing it)

  28. #28
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    Either use the code that was supplied by other members to sort the elements or place the elements into a listbox and have them sorted automatically (you would need to convert the numbers to strings first for the listbox).

  29. #29
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: sorting arrays

    Here:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim Order(0 To 4) As Integer, K As Long
    5.    
    6.     Order(0) = 3
    7.     Order(1) = 1
    8.     Order(2) = 4
    9.     Order(3) = 0
    10.     Order(4) = 2
    11.    
    12.     QuickSort Order, LBound(Order), UBound(Order) ' Sort ascending
    13.     ReverseArray Order ' reorder, so it will be descending
    14.    
    15.     ' print result
    16.     For K = LBound(Order) To UBound(Order)
    17.         Debug.Print "Order(" & K & ") = " & Order(K)
    18.     Next K
    19. End Sub
    20.  
    21. Private Sub ReverseArray(A() As Integer)
    22.     Dim K As Long, Elements As Long
    23.     Dim LB As Long, UB As Long
    24.    
    25.     LB = LBound(A)
    26.     UB = UBound(A)
    27.    
    28.     Elements = UB - LB
    29.     For K = 0 To Elements \ 2
    30.         Swap A(LB + K), A(UB - K)
    31.     Next K
    32. End Sub
    33.  
    34. Private Sub QuickSort(C() As Integer, ByVal First As Long, ByVal Last As Long)
    35.     '
    36.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    37.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=231925[/url]
    38.     '
    39.     Dim Low As Long, High As Long
    40.     Dim MidValue As Integer
    41.    
    42.     Low = First
    43.     High = Last
    44.     MidValue = C((First + Last) \ 2)
    45.    
    46.     Do
    47.         While C(Low) < MidValue
    48.             Low = Low + 1
    49.         Wend
    50.        
    51.         While C(High) > MidValue
    52.             High = High - 1
    53.         Wend
    54.        
    55.         If Low <= High Then
    56.             Swap C(Low), C(High)
    57.             Low = Low + 1
    58.             High = High - 1
    59.         End If
    60.     Loop While Low <= High
    61.    
    62.     If First < High Then QuickSort C, First, High
    63.     If Low < Last Then QuickSort C, Low, Last
    64. End Sub
    65.  
    66. Private Sub Swap(ByRef A As Integer, ByRef B As Integer)
    67.     Dim T As Integer
    68.    
    69.     T = A
    70.     A = B
    71.     B = T
    72. End Sub

  30. #30

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    How do I get the values of the array into the listbox? And I presume I'm correct by modifying the code to "dim order(4) as string"? (randem)

  31. #31
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    use List1.AddItem sStr (You have to place a listbox on your form first). And you convert the number to a string, you do not have to define it as a string.

    ex.
    VB Code:
    1. List1.AddItem Format(Order(n),"00000")

  32. #32

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    Good, thats working, but how would I remove the items in the listbox before the next additem? I have tried "list1.clear" but that doesnt work: it only shows one value after that, even if there's more than one value being added

  33. #33
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    What???,

    Don't you want to add all the items to the listbox in order to get them sorted????

    Post some code...

  34. #34
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: sorting arrays

    Why don't you use my code ? you don't have to convert to String !

  35. #35

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    VB Code:
    1. 'after pressing a button (with extra code above)
    2. For i = 0 To 4
    3. List1.Clear
    4. List1.AddItem Format(order(i), "")
    5. Next
    Thats the code i'm using.
    The thing is, is this code getting all 5 parts of the array 'order()'?
    Order() , as I have stated, has a random number in each part of it, so there are 5 random numbers in it.

    If I put a msgbox below saying 'list1.listcount' it only has recorded 1 variable

  36. #36
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: sorting arrays

    Because you are clearing it 4 times. try this:

    VB Code:
    1. 'after pressing a button (with extra code above)
    2. List1.Clear
    3. For i = 0 To 4
    4.   List1.AddItem Format(order(i), "")
    5. Next

  37. #37

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: sorting arrays

    i have a slight problem with that. I have 5 'hold' checkboxes, so I would need a way to only clear the things that have not been held, and not do anything about the others

    Is there a '.clear' option which you can specify which entry you want to delete?

  38. #38
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: sorting arrays

    ajames,

    Your format statement will give you inacurate sorts if your numbers are longer than 1 character.

    Things that have not been held???? If you do not put the items in the listbox would that be better?

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