Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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


--
jimmyp
------------------------------------------------------------------------
jimmyp's Profile: http://www.excelforum.com/member.php...o&userid=32217
View this thread: http://www.excelforum.com/showthread...hreadid=522186

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default sort function - from ascending to descending

Hi
try changing the test
If List(i) List(j)
to
If List(i) < List(j)

(untested)
Paul

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default sort function - from ascending to descending


i tried that - it almost works :) but misses of the largest element, ie
the one that should be at the start...the rest are all in descending
order though


--
jimmyp
------------------------------------------------------------------------
jimmyp's Profile: http://www.excelforum.com/member.php...o&userid=32217
View this thread: http://www.excelforum.com/showthread...hreadid=522186

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default sort function - from ascending to descending

It should work as demonstrated he

Sub BubbleDown()
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer
Dim Temp As Integer
List = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
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
For i = First To Last
Debug.Print i, List(i)
Next
End Sub

--
Regards,
Tom Ogilvy


"jimmyp" wrote:


i tried that - it almost works :) but misses of the largest element, ie
the one that should be at the start...the rest are all in descending
order though


--
jimmyp
------------------------------------------------------------------------
jimmyp's Profile: http://www.excelforum.com/member.php...o&userid=32217
View this thread: http://www.excelforum.com/showthread...hreadid=522186


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default sort function - from ascending to descending


tom essentially thats the same as what i posted except you have 'List =
Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)' whereas i pass in my array from
another sub. Thing is its still missing the first element, but if i
change back to then it displays all the elements, just in the wrong
order... its odd the only thing i change is the <.


--
jimmyp
------------------------------------------------------------------------
jimmyp's Profile: http://www.excelforum.com/member.php...o&userid=32217
View this thread: http://www.excelforum.com/showthread...hreadid=522186



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default sort function - from ascending to descending

that is correct - I was showing you that it does work to reverse the order of
an array.

The only problem I can think of is that you recalcitrant value is being
stored as a string rather than a number. As long as the string is never
moved, you won't get an error. If your code does try to move it, then it
will raise an error since temp is Dim'd as Integer.

--
Regards,
Tom Ogilvy


"jimmyp" wrote:


tom essentially thats the same as what i posted except you have 'List =
Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)' whereas i pass in my array from
another sub. Thing is its still missing the first element, but if i
change back to then it displays all the elements, just in the wrong
order... its odd the only thing i change is the <.


--
jimmyp
------------------------------------------------------------------------
jimmyp's Profile: http://www.excelforum.com/member.php...o&userid=32217
View this thread: http://www.excelforum.com/showthread...hreadid=522186


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Column sort with Ascending / Descending Arrows JT Excel Worksheet Functions 3 May 18th 08 02:40 AM
Ascending Sort formula, change to neg #: descending sort.. nastech Excel Discussion (Misc queries) 6 July 2nd 07 11:00 PM
How to sort out (ascending or descending) automatically a list PF Excel Worksheet Functions 1 December 13th 06 03:27 PM
Sort other than by alphabetical ascending/ descending Melissa Excel Discussion (Misc queries) 6 September 2nd 05 07:25 AM
how can I hide sort ascending and sort descending options in the . vida Excel Discussion (Misc queries) 0 December 11th 04 12:31 AM


All times are GMT +1. The time now is 02:40 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"