Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Mar 2007
    Posts
    3

    Unanswered: sorting a 2-dimensional array

    i have created a simple game and am adding a high score table to it, i was wondering how i would go about sorting the array using only the second column... the table is arranged as :
    name1 score1
    name2 score2
    name3 score3
    and so on any help would be much appreciated

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Familiar with SQL?

    SELECT name, score FROM YourTable ORDER BY score
    George
    Home | Blog

  3. #3
    Join Date
    Mar 2007
    Posts
    3
    nope i'm a bit of a movice when it comes to vb

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Here's a nice SQL tutorial that will be the most useful thing you ever read: http://www.w3schools.com/sql/default.asp

    Ever used queries? They're written in SQL!

    SQL stands for Simple/Structured Query Language which is what you use when you want to ask the database questions
    E.g.
    "Can you tell me all the high scores and who scored them in ascending order, please?"
    "Can you tell me the users with scores that are greater than 50?"
    "Can you tell me all users called 'Jeff'?"
    ...and so on and so forth.
    George
    Home | Blog

  5. #5
    Join Date
    Mar 2007
    Posts
    3
    thanks very much i'll try to impliment all that tomorrow next time i'm at the thing

  6. #6
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    for a "simple game" database access may be a bit of an overkill.

    Also, since a high scores list should be fairly small (10-20 rows or so) a simple bubble sort would be as fast as a quicksort.

    Search Google for "Bubble Sort" and you'll find a gazillion hits. Essentially, a bubble sort is a nested loop.

    In the outer loop, you are looping from the array lower bound to the array upper bound-1. In the inner loop, you are looping from the current value of the outer loop to the array upper bound. Within the loop, you compare the array elements, and swap if the lower element is greater than the higher element.

    pseudo code:
    Code:
    Dim aryScore (1,9) ' 2x10 array
    For I=LBound(aryScore,2) to UBound(aryScore,2) -1
      For N = I to UBound (aryScore,2)
        If aryScore(1,I)>aryScore(N,I) then
          ' Swap the array elements
        EndIf
      Next N
    Next I
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I forget; does access support SELECT TOP queries?
    George
    Home | Blog

  8. #8
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by georgev
    I forget; does access support SELECT TOP queries?
    It certainly does ... SELECT TOP 10 Whatever FROM Whereever WHERE (SomeCondition=True);
    Back to Access ... ADO is not the way to go for speed ...

  9. #9
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Top 10 scores:
    Code:
    SELECT TOP 10 Name, Score FROM MyTable ORDER BY Score ASC
    George
    Home | Blog

  10. #10
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by georgev
    Top 10 scores:
    Code:
    SELECT TOP 10 Name, Score FROM MyTable ORDER BY Score ASC
    Uh ... George. You got that wrong ... This query will return the 1st ten names with their scores ordered in ascending order ...

    Perhaps: SELECT TOP 10 Score, Name FROM MyTable ORDER BY Score DESC
    Back to Access ... ADO is not the way to go for speed ...

  11. #11
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    The DESC makes a difference but I'm unconvinced by the physical order of the select statement.

    I'm sure the order statement is 'run' first and the top 10 are then returned...
    *calls for Rudy*
    George
    Home | Blog

  12. #12
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by georgev
    The DESC makes a difference but I'm unconvinced by the physical order of the select statement.

    I'm sure the order statement is 'run' first and the top 10 are then returned...
    *calls for Rudy*
    Nooooooooo. Look it up in Help. TOP works on a single column (the 1 to the immediate right of it). You are correct that the result set is ordered THEN the TOP is applied ... BUT, (very big one indeed) you are using the ASCending order (from LOW to HIGH) so, you're not getting the result you want anyways ...

    P.S. : When using TOP Ordering of the columns is of paramount importance
    Back to Access ... ADO is not the way to go for speed ...

  13. #13
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Ok fine, be like that
    Yeah, you're right, I'm wrong...
    You're smart, I'm du.. done!

    I knew I would regret posting some SQL I wrote in a matter of milliseconds!
    DESC - correct.
    However I'm still confused by the TOP with ORDER clause... I thought it simply picked up the top n rows which are produced by the ordering.
    If you sorted it by score
    6000, 5000, 4500, 4499, 3000... etc.
    then selected the top 3 it'd return
    6000, 5000, 4500.

    That was my logic anyway
    George
    Home | Blog

  14. #14
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by georgev
    Ok fine, be like that
    Yeah, you're right, I'm wrong...
    You're smart, I'm du.. done!

    I knew I would regret posting some SQL I wrote in a matter of milliseconds!
    DESC - correct.
    However I'm still confused by the TOP with ORDER clause... I thought it simply picked up the top n rows which are produced by the ordering.
    If you sorted it by score
    6000, 5000, 4500, 4499, 3000... etc.
    then selected the top 3 it'd return
    6000, 5000, 4500.

    That was my logic anyway
    TOP works ONLY on the column associated with it ... This would be an interesting test ... With a DESC order, what would be returned???? Say records of:
    Code:
    Ziggy            1000
    Yabba           2000
    Wazzoo         3000
    Vern             4000
    Xandu           5000
    Ummmma       6000
    Cornfed         7000
    DingDong       8000
    WhoIs           9000
    Cokehead     10000
    Whiner         11000
    Plastered      12000
    Back to Access ... ADO is not the way to go for speed ...

  15. #15
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    SELECT TOP 5 MyTable.Name, MyTable.Score
    FROM MyTable
    ORDER BY MyTable.Score DESC;
    Code:
    Name	Score
    Plastered	12000
    Whiner	11000
    Cokehead	10000
    WhoIs	9000
    DingDong	8000
    [BOAST]Looks like I was right [/BOAST]
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •