Sorter i 2 Dim Array
Jeg er ikke en super helt i ASP og jeg har prøvet at kopiere og tilrette denne sortering funktion http://www.4guysfromrolla.com/webtech/012799-3.shtmlJeg vil gerne ha den til at sorter fra høje tal og ned og ikke fra små tal og størrer.
Og her er min kode
<%@ Language="VBScript" %>
<!--#include file="Data.asp" -->
<%
' Databaseforbindelse - husk at angive sti til din database
Set Conn = Server.CreateObject("ADODB.Connection")
Set RsGameResult = Server.CreateObject("ADODB.Recordset")
Set RsPlayer = Server.CreateObject("ADODB.Recordset")
Conn.Open DSN
SQLGameResult = "SELECT * FROM GameResults Where CurrSeason = TRUE"
RsGameResult.Open SQLGameResult, DSN, 2
' Setting up Competion year and type
GameYearComp = Left(RsGameResult("GameNo"),6)
GameYear = Left(GameYearComp, 4)
GameComp = Right(GameYearComp, 2)
GameYearInt = CInt(GameYear) + 1
AmountofGames = 0
Do While Not RsGameResult.EOF
AmountofGames = AmountofGames + 1
RsGameResult.MoveNext
Loop
TeamNo = AmountofGames/2
RsGameResult.MoveFirst
Dim GameLeague()
redim preserve GameLeague(TeamNo,3)
' **** Populating Team into Array ****
GameLeague(0,0) = "Harlequins"
ExNumber = 1
Do While Not RsGameResult.EOF
If RsGameResult("HomeTeam") = "Harlequins" Then
GameLeague(ExNumber,0) = RsGameResult("AwayTeam")
ExNumber = ExNumber + 1
End If
RsGameResult.MoveNext
Loop
For i = 0 To TeamNo
Response.Write "Team" & i & " : " & GameLeague(i,0) & "<br>"
Next
Response.write TeamNo
RsGameResult.MoveFirst
' **** Populating Team Home Results into Array ****
For i = 0 To TeamNo
Do While Not RsGameResult.EOF
If RsGameResult("HomeTeam") = GameLeague(i,0) Then
GameLeague(i,1) = RsGameResult("HomeTeamScore") + GameLeague(i,1)
GameLeague(i,2) = GameLeague(i,2) + 1
End If
RsGameResult.MoveNext
Loop
RsGameResult.MoveFirst
Next
RsGameResult.MoveFirst
' **** Populating Team Away Results into Array ****
For i = 0 To TeamNo
Do While Not RsGameResult.EOF
If RsGameResult("AwayTeam") = GameLeague(i,0) Then
GameLeague(i,1) = RsGameResult("AwayTeamScore") + GameLeague(i,1)
GameLeague(i,2) = GameLeague(i,2) + 1
End If
RsGameResult.MoveNext
Loop
RsGameResult.MoveFirst
Next
RsGameResult.MoveFirst
'==-----------------------------------------------------------==
'== This entire piece of code was shamelessly stolen from ==
'== the 4 Guys From Rolla WebWeekly newsletter, translated ==
'== to VBScript and changed into server-side ASP code. ==
'== Every effort has been made to keep comments intact. ==
'== ==
'== This version sorts 2-dimensional arrays on a single field ==
'==-----------------------------------------------------------==
Response.Write "<HTML><HEAD></HEAD><BODY BGCOLOR=""WHITE"">"
Sub SwapRows(ary,row1,row2)
'== This proc swaps two rows of an array
Dim x,tempvar
For x = 0 to Ubound(ary,2)
tempvar = ary(row1,x)
ary(row1,x) = ary(row2,x)
ary(row2,x) = tempvar
Next
End Sub 'SwapRows
Sub QuickSort(vec,loBound,hiBound,SortField)
'==--------------------------------------------------------==
'== Sort a 2 dimensional array on SortField ==
'== ==
'== This procedure is adapted from the algorithm given in: ==
'== ~ Data Abstractions & Structures using C++ by ~ ==
'== ~ Mark Headington and David Riley, pg. 586 ~ ==
'== Quicksort is the fastest array sorting routine for ==
'== unordered arrays. Its big O is n log n ==
'== ==
'== Parameters: ==
'== vec - array to be sorted ==
'== SortField - The field to sort on (2nd dimension value) ==
'== loBound and hiBound are simply the upper and lower ==
'== bounds of the array's 1st dimension. It's probably ==
'== easiest to use the LBound and UBound functions to ==
'== set these. ==
'==--------------------------------------------------------==
Dim pivot(),loSwap,hiSwap,temp,counter
Redim pivot (Ubound(vec,2))
'== Two items to sort
if hiBound - loBound = 1 then
if vec(loBound,SortField) > vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound)
End If
'== Three or more items to sort
For counter = 0 to Ubound(vec,2)
pivot(counter) = vec(int((loBound + hiBound) / 2),counter)
vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)
vec(loBound,counter) = pivot(counter)
Next
loSwap = loBound + 1
hiSwap = hiBound
do
'== Find the right loSwap
while loSwap < hiSwap and vec(loSwap,SortField) <= pivot(SortField)
loSwap = loSwap + 1
wend
'== Find the right hiSwap
while vec(hiSwap,SortField) > pivot(SortField)
hiSwap = hiSwap - 1
wend
'== Swap values if loSwap is less then hiSwap
if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)
loop while loSwap < hiSwap
For counter = 0 to Ubound(vec,2)
vec(loBound,counter) = vec(hiSwap,counter)
vec(hiSwap,counter) = pivot(counter)
Next
'== Recursively call function .. the beauty of Quicksort
'== 2 or more items in first section
if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField)
'== 2 or more items in second section
if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField)
End Sub 'QuickSort
Sub PrintArray(vec,lo,hi,mark)
'==-----------------------------------------==
'== Print out an array from the lo bound ==
'== to the hi bound. Highlight the column ==
'== whose number matches parm mark ==
'==-----------------------------------------==
Dim i,j
Response.Write "<table border=""1"" cellspacing=""0"">"
For i = lo to hi
Response.Write "<tr>"
For j = 0 to Ubound(vec,2)
If j = mark then
Response.Write "<td bgcolor=""FFFFCC"">"
Else
Response.Write "<td>"
End If
Response.Write vec(i,j) & "</td>"
Next
Response.Write "</tr>"
Next
Response.Write "</table>"
End Sub 'PrintArray
Randomize
Dim x(9,5),z,y
Const col = 0
For z = 0 to 9
For y = 0 to 5
x(z,y) = int(Rnd*1000)
If (Rnd < 0.5) then x(z,y) = x(z,y)-1000
Next
Next
Call QuickSort(GameLeague,0,3,1)
Call PrintArray(GameLeague,0,3,1)
Response.Write "</BODY></HTML>"
%>