Results 1 to 4 of 4

Thread: sort function - from ascending to descending

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    5

    sort function - from ascending to descending

    hi

    i have this bubblesort sub -

    Code:
    Sub BubbleSort(List() As Integer)
    
        Dim First As Integer, Last As Integer
        Dim i As Integer, j As Integer
        Dim Temp As Integer
        
        First = LBound(List)
        Last = UBound(List)
        For i = First To Last - 1
            For j = i + 1 To Last
                If List(i) > List(j) Then
                    Temp = List(j)
                    List(j) = List(i)
                    List(i) = Temp
                End If
            Next j
        Next i
    End Sub
    but at the moment it sorts all the elements in ascending order, and i need it biggest element first ie. descending...

    any idea how i change it, i guess it would be simple but i suppose it may need completely rewriting

    thanks


    jimmyp

  2. #2
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: sort function - from ascending to descending

    You just had to change one sign:

    VB Code:
    1. Option Explicit
    2.  
    3. Sub BubbleSort(List() As Integer)
    4.  
    5.     Dim First As Integer, Last As Integer
    6.     Dim i As Integer, j As Integer
    7.     Dim Temp As Integer
    8.    
    9.     First = LBound(List)
    10.     Last = UBound(List)
    11.     For i = First To Last
    12.         For j = i + 1 To Last
    13.             If List(i) < List(j) Then 'This changed, was ">"
    14.                 Temp = List(j)
    15.                 List(j) = List(i)
    16.                 List(i) = Temp
    17.             End If
    18.         Next j
    19.     Next i
    20.    
    21.     For i = First To Last
    22.     Debug.Print List(i)
    23.     Next
    24. End Sub
    25.  
    26. Sub GiveList()
    27. Dim List(0 To 4) As Integer
    28.  
    29. List(0) = 5
    30. List(1) = 7
    31. List(2) = 1
    32. List(3) = 11
    33. List(4) = 8
    34.  
    35. Call BubbleSort(List())
    36.  
    37. End Sub




    If you find any of my posts of good help, please rate it

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: sort function - from ascending to descending

    Here's a sort functionn that allows you to sort either Ascending or Descending. It can also sort a 2D array, based on a selected column.


    VB Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. ' Comments:     This function sorts an array based on a column
    3. '               number, in either ascending or descending order.
    4. '
    5. ' Arguments:    OldArray    The array to be sorted
    6. '               ColNum      An optional column number for 2d
    7. '                           arrays.
    8. '               ASC         An optional boolean value to sort
    9. '                           in ASCending or descending order.
    10. '
    11. ' Date          Developer       Action
    12. ' --------------------------------------------------------------
    13. ' 09/04/05      Declan Kenny    Initial version
    14. '
    15. Function SortArray(ByRef OldArray As Variant, Optional ByVal ColNum As Integer = 1, Optional ByVal ASC As Boolean = True)
    16.  
    17. Dim Sorted As Boolean
    18. Dim ArrayRec As Integer
    19. Dim temp As Variant
    20. Dim ColCount As Integer
    21.  
    22.     Sorted = False
    23.     Do While Not Sorted
    24.         Sorted = True
    25.         If ASC Then
    26.             For ArrayRec = 1 To UBound(OldArray, ColNum) - 1
    27.                 If OldArray(ArrayRec, ColNum) > OldArray(ArrayRec + 1, ColNum) Then
    28.                     For ColCount = 1 To UBound(OldArray, 2)
    29.                         temp = OldArray(ArrayRec + 1, ColCount)
    30.                         OldArray(ArrayRec + 1, ColCount) = OldArray(ArrayRec, ColCount)
    31.                         OldArray(ArrayRec, ColCount) = temp
    32.                     Next ColCount
    33.                     Sorted = False
    34.                 End If
    35.             Next ArrayRec
    36.         Else
    37.              For ArrayRec = 2 To UBound(OldArray, ColNum)
    38.                 If OldArray(ArrayRec, ColNum) > OldArray(ArrayRec - 1, ColNum) Then
    39.                     For ColCount = 1 To UBound(OldArray, 2)
    40.                         temp = OldArray(ArrayRec - 1, ColCount)
    41.                         OldArray(ArrayRec - 1, ColCount) = OldArray(ArrayRec, ColCount)
    42.                         OldArray(ArrayRec, ColCount) = temp
    43.                     Next ColCount
    44.                     Sorted = False
    45.                 End If
    46.             Next ArrayRec
    47.         End If
    48.     Loop
    49. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: sort function - from ascending to descending

    Nice work Declan ... even if it is old. However, I tried to use it but found that it is SLOW, SLOW, SLOW !!!

    On my machine, sorting 2 columns - the sort column with 10 digit numbers, and one with about 50 characters of text - the function execution time is tightly fit by:

    Time (sec) = 0.000002 * NumRows

    A few thousand rows and you will be waiting for completion. I have over 28,000 rows in my data set ... it is MUCH faster just to copy the 2 columns into a clean sheet and use the Excel WorksheetFunction sort to do the work in less than a second (even with keeping ScreenUpdating ON!)

    This is NOT a criticism ... just a hint to add to the bottom of this thread for anybody who discovers it in the future. As always, thanks Declan for your great support on this forum.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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