Avatar billede 44nato44 Nybegynder
08. juli 2007 - 21:10 Der er 5 kommentarer og
1 løsning

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.shtml

Jeg 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>"
%>
Avatar billede eagleeye Praktikant
08. juli 2007 - 22:48 #1
Jeg har et link til et sort script som har taget udgangspunkt i det script du har vist og linket til. Forskellen er blandet andet at der er kommet en sort order på så man kan sortere stigne eller faldene:

http://www.evolt.org/article/Quick_and_Dirty_ASP_Array_Sorting/17/21724/index.html


Prøv at kopier koden fra ***Quick Sort v2*** boksen.

Så kan du rette dine to linier:


Call QuickSort(GameLeague,0,3,1)
Call PrintArray(GameLeague,0,3,1)


til dette:

Call QuickSort(GameLeague,0,3,1,"DESC")  'Sdiste param angiver sortorder enten "ASC" eller "DESC"
Call PrintArray(GameLeague,0,3,1)
Avatar billede 44nato44 Nybegynder
08. juli 2007 - 23:36 #2
Tusinde tak
Avatar billede bauerdata Nybegynder
09. juli 2007 - 01:11 #3
Er det sådan her du mener det skal se ud ?
http://www.databassen.dk/cgi-bin/arraysort
Avatar billede djmoose Nybegynder
10. august 2007 - 15:45 #4
Avatar billede djmoose Nybegynder
10. august 2007 - 15:46 #5
Forkert... sorry
Avatar billede bauerdata Nybegynder
10. august 2007 - 16:53 #6
Hvordan skal det så være
Skal der ikke sorteres efter f. eks kolonne 4
Arrays =[
[ 1, 2, 3, 4, 5 ]
[ 2, 3, 4, 5, 7 ]
[ 3, 4, 5, 3, 8 ]
[ 4, 5. 3, 1, 9 ]
]
Hvordan vil du have ovenstående sorteret ?
Kan du vise mig resultatet.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester