| Welcome to Webforumz.com. |
|
Nov 23rd, 2007, 08:36
|
#1 (permalink)
|
|
Section Manager - Website Critique
Join Date: May 2007
Location: inside the outside
Posts: 1,094
|
[SOLVED] Array sorting
I am still learning ASP functions, so please be gentle with me. I have got a script that sorts a multi-dimensional array Ascending, but I need to switch this to Descending.
Anyway, I've gone and got myself all confuddled, and can't see the wood for the trees. Any help is hugely appreciated.
- Code: Select all
Function arraySortDecend( arToSort, sortBy, compareDates )
Dim c, d, e, smallestValue, smallestIndex, tempValue
For c = 0 To uBound( arToSort, 2 ) - 1
smallestValue = arToSort( sortBy, c )
smallestIndex = c
For d = c + 1 To uBound( arToSort, 2 )
if not compareDates then
if strComp( arToSort( sortBy, d ), smallestValue ) < 0 Then
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
else
if not isDate( smallestValue ) then
arraySort = arraySort( arToSort, sortBy, false)
exit function
else
if dateDiff( "d", arToSort( sortBy, d ), smallestValue ) > 0 Then
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
end if
end if
Next
if smallestIndex <> c Then 'swap
For e = 0 To uBound( arToSort, 1 )
tempValue = arToSort( e, smallestIndex )
arToSort( e, smallestIndex ) = arToSort( e, c )
arToSort( e, c ) = tempValue
Next
End if
Next
End Function
Last edited by welshstew; Nov 23rd, 2007 at 08:37.
Reason: no asp tag for code...
|
|
|
Nov 27th, 2007, 00:03
|
#2 (permalink)
|
|
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
|
Re: Array sorting
Sorry welshstew, I don't know array in asp  I am new to asp as well.. 
Maybe Eran can help?
__________________
|
|
|
Nov 27th, 2007, 18:06
|
#3 (permalink)
|
|
Junior Member
Join Date: Aug 2007
Location: Haverhill
Posts: 40
|
Re: Array sorting
You say your code currently sorts the array ascending
I believe but have not checked nor properly read your code that the following modified code will work.
- Code: Select all
Function arraySortDecend( arToSort, sortBy, compareDates )
Dim c, d, e, smallestValue, smallestIndex, tempValue
For c = 0 To uBound( arToSort, 2 ) - 1
smallestValue = arToSort( sortBy, c )
smallestIndex = c
For d = c + 1 To uBound( arToSort, 2 )
if not compareDates then
if strComp( smallestValue, arToSort( sortBy, d ) ) < 0 Then
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
else
if not isDate( smallestValue ) then
arraySort = arraySort( arToSort, sortBy, false)
exit function
else
if dateDiff( "d", smallestValue, arToSort( sortBy, d ) ) > 0 Then
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
end if
end if
Next
if smallestIndex <> c Then 'swap
For e = 0 To uBound( arToSort, 1 )
tempValue = arToSort( e, smallestIndex )
arToSort( e, smallestIndex ) = arToSort( e, c )
arToSort( e, c ) = tempValue
Next
End if
Next
End Function
I cannot stress enough that this is just my cursory glance at it.
Personally when I am trying to create a piece of code I start out writing out how I would do it manually in simple english then work on converting that to code with some refinements along the way.
I find that it helps my understanding of what I'm doing.
|
|
|
Nov 28th, 2007, 13:56
|
#4 (permalink)
|
|
Junior Member
Join Date: Oct 2007
Location: Israel
Age: 20
Posts: 31
|
Re: Array sorting
i found his code on google as a step by step guide thats explain what every row doing and where you need to change the code to Descending.
I can do it to but i need to run some tests to check if its work and i`m kinda lazzy :P
you can make a new array and get all the asc array to there one by one in a loop or you can loop the array backwards... or you can find and read the script writer guide.
|
|
|
Nov 28th, 2007, 14:07
|
#5 (permalink)
|
|
Section Manager - Website Critique
Join Date: May 2007
Location: inside the outside
Posts: 1,094
|
Re: Array sorting
Thanks for the help guys, will check the script writier guide, if you could point a link to me.
Thanks again.
Stew
|
|
|
Nov 28th, 2007, 15:50
|
#6 (permalink)
|
|
Junior Member
Join Date: Oct 2007
Location: Israel
Age: 20
Posts: 31
|
Re: Array sorting
Last edited by Eran; Nov 28th, 2007 at 15:52.
|
|
|
Nov 28th, 2007, 16:45
|
#7 (permalink)
|
|
Section Manager - Website Critique
Join Date: May 2007
Location: inside the outside
Posts: 1,094
|
Re: Array sorting
Thanks Eran, I think I have it sorted now. At least it is working for me at the mo.
- Code: Select all
Function arraySortDecend( arToSort, sortBy, compareDates )
Dim c, d, e, smallestValue, smallestIndex, tempValue
'loops the array
For c = 0 To uBound( arToSort, 2 ) - 1
'assign current value as smallest value
smallestValue = arToSort( sortBy, c )
smallestIndex = c
'next array item
For d = c + 1 To uBound( arToSort, 2 )
if not compareDates then
'take this item and compare to previous item
if strComp( arToSort( sortBy, d ), smallestValue ) < 0 Then
'grab the smallest value
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
else
if not isDate( smallestValue ) then
arraySort = arraySort( arToSort, sortBy, false)
exit function
else
if dateDiff( "d", smallestValue, arToSort( sortBy, d ) ) > 0 Then
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
end if
end if
Next
if smallestIndex <> c Then 'swap the index
For e = 0 To uBound( arToSort, 1 )
tempValue = arToSort( e, smallestIndex )
arToSort( e, smallestIndex ) = arToSort( e, c )
arToSort( e, c ) = tempValue
Next
End if
Next
End Function
and then this is the array sort parameter:
- Code: Select all
'sort the array
arraySortDecend pressReleases, 2, true
Thanks all for the help.
Stew
|
|
|
| Thread Tools |
|
|
| Rate This Thread |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|