Trying to sort an array

Get help using Construct 2

Post » Wed Aug 20, 2014 11:31 pm

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.
B
9
S
1
Posts: 97
Reputation: 715

Post » Wed Aug 20, 2014 11:54 pm

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.
You do not have the required permissions to view the files attached to this post.
ImageImageImage
B
71
S
22
G
247
Posts: 3,753
Reputation: 137,552

Post » Wed Aug 20, 2014 11:56 pm

You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Aug 21, 2014 12:05 pm

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?
B
9
S
1
Posts: 97
Reputation: 715

Post » Thu Aug 21, 2014 12:21 pm

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!
B
9
S
1
Posts: 97
Reputation: 715

Post » Thu Aug 21, 2014 12:59 pm

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 :)
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Aug 21, 2014 3:49 pm

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.
B
9
S
1
Posts: 97
Reputation: 715

Post » Thu Aug 21, 2014 9:50 pm

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.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Aug 21, 2014 10:04 pm

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!
B
9
S
1
Posts: 97
Reputation: 715

Post » Thu Aug 21, 2014 10:09 pm

neutrinox wrote: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).
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 11 guests