Trying to sort an array

0 favourites
  • 12 posts
From the Asset Store
A master sorting Puzzle fun and addictive puzzle game! A challenging yet relaxing game to exercise your brain!
  • Hi!

    I'm trying to make a "top three" list for my game.

    During the game, I store points in a 2d-array. The X-dimension has the player name stored. The Y-dimension stores the points.

    When the player goes to the highScore layout, I'm want to display a top three list in which the player with the highest score is first, then comes the player with the second highest score and so on.

    My tactic (which doesn't seem to work) was to sort the array based on it's Y axis and then reverse it so that the order is descending. I'm not sure if I understand correclty how sorting works though. If I have an array like this:

    [john,2][mike,5][tom,1]

    and I sort it and then reverse it on the Y-axis, shouldn't the result be:

    [mike,5][john,2][tom,1]

    Or is that not how sorting works?

    My next step after sorting and reversing is to do a for-each loop for the array in which I fill the top three text slots in the order of the array. But this concept doesn't seem to work because all the names will be in their original order, not in the order I thought I achieved by sorting them.

  • That's not really the correct way to think of it. X is each record. Y=0 is the name, Y=1 is the score.

    The simplest solution is to switch the order of the data. Put the score in Y=0, and name in Y=1. Then sort in X and Reverse X. That will get you what you want.

  • hope this helps

  • Thanks for your replies!

    Do you understand what actually happens when I sort the array Y-axis? Oh maybe I actually get it now, is it like this:

    Before sorting Y:

    y0 values: a, b, c, d, e, f

    y1 values: 1, 2, 3, 4, 5

    y2 values: 6, 7, 8, 9, 10

    After Sorting Y:

    y0 values: 1, 2, 3, 4, 5

    y1 values: 6, 7, 8, 9, 10

    y2 values: a, b, c, d, e, f

    In other words, is the sorting vertical? Is there no way to sort the Y-axis horizontally?

  • Hmmm... maybe there is a way, even if the values to be sorted are not on the y0-axis.. What if you temporarily switch y-rows so that the row you need to sort is y0... I'm sot sure if such a switch is possible, but I think that would solve the problem!

  • The thing is, sorting on the X axis of a 2D array preserves the column data, but sorting on the Y axis does not preserve the row data.

    The other thing is you've said 'X stores the names, y stores the points' but this doesn't make sense. You're storying names AND scores in both X and Y dimensions. What you meant is you're storing names in row y=0 and scores in row y=1.

    Anyway what you said in your last post is true. If you had the scores in row 0, then sorting on X would work.

    Edit: realised I pretty much repeated what BH said. Anyway, just use one of the capx's provided above

  • Hi!

    You are right, I expressed myself incorrectly when I was talking about storing values in the x-axis. I should have said "y0" instead.

    I think I will still experiment in doing a quick y-order swap before doing the sorting, just because my project has been built to depend on the names being on y0 and I would need to find and replace many parts of my project.

    I guess I can just build something like this (pseudo code), perhaps inside a fuction:

    on Function "ySwap"

    For each item at y0, store them in a temporary array at y0. For each item at y1 store them in a temporary array at y1. For each item at temporary array y0, store them at real array y1. For each item at temporary array y1 store them at real array y0. Then do the sorting and put the names in place. Finally run the function again.

    I think I will give that a try because my project is so complicated and I'm scare of messing it up if I start changing the older logic in which names are at y0.

  • In my capx, the names are also at y=0.

    But even if they weren't, you just switch around the 0's and 1's, and voila. And if you want ascending sort order, change the < to > in event 7.

  • Thanks codah, I didn't realize that because I apparently only checked the example from the thread you linked. I'll try your system now!

  • Thanks codah, I didn't realize that because I apparently only checked the example from the thread you linked. I'll try your system now!

    Hey no problem. You can throw it into a function, but unfortunately we can't make a 'generic' function for any array because you can't pick an Array like you can a Sprite (and it doesn't make sense because there is only one instance).

  • Your way worked nicely! I ended up storing the original array in a temp array though, because in this particular example I needed to alter the order of the array only for the top-list and then revert it back. Thanks for your help!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Your way worked nicely! I ended up storing the original array in a temp array though, because in this particular example I needed to alter the order of the array only for the top-list and then revert it back. Thanks for your help!

    No problem. I also gained more insight into C2 arrays

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)