# How do I sort a 2D array?

Get help using Construct 2

### » Sat Mar 01, 2014 12:33 am

I would like to sort the second column, in a 5x50 array... for example:

yx 1 2 3 4 5
1 Matt 1345 Car 56 Green
2 Chris 2345 Truck 93 Red
3 Laura 23255 Train 15 Blue
4 Ashley 92234 Bike 134 Yellow
5 Sam 7346 Plane 99 Purple
etc etc

I would like it to sort like this:

yx 1 2 3 4 5
1 Ashley 92234 Bike 134 Yellow
2 Laura 23255 Train 15 Blue
3 Sam 7346 Plane 99 Purple
4 Chris 2345 Truck 93 Red
5 Matt 1345 Car 56 Green
etc etc

As you can see, the second column is sorted in order largest to smallest. I've seen examples where people have zeropadded the scores, and added them into just one line, and then sorting that way... but that just seems extremely inefficient... there has to be a better way. Is that not how these array's work? Can you not move a line, for example, All of Y1 (X1, X2, X3, X4, X5) and insert before Y4, thus pushing Y2, Y3, to spot Y1, and Y2 giving what was line Y1 the spot line Y3 keeping all of it's X axis's?

I tell you.. it's so much easier doing an online leaderboard, than a local one let me tell ya.
B
47
S
15
G
3
Posts: 201
Reputation: 4,625

### » Wed May 07, 2014 3:07 pm

I'm interested in ordering an array from highest to lowest as well for a leaderboard table, so I too would like to know how to do this.

At the moment I'm toying with checking if each number is higher than the next, and if it is, move the array over to a new one, and delete the line from the old array and repeating, but it's not working as planned.

I'm still amazed we don't have a function like "Randomize array" or "Sort Array by descending" so it's actually useful for scoreboards.

*In addition, I think it would be useful to be able to choose which column of the array you'd like to sort by. For example, maybe I want the player's names to be alphabetical, or even part of their score. At the moment, we are limited to the very first part of each array.
B
97
S
20
G
8
Posts: 1,062
Reputation: 12,725

### » Wed May 07, 2014 8:03 pm

AnD4D wrote:I'm still amazed we don't have a function like "Randomize array" or "Sort Array by descending" so it's actually useful for scoreboards.

*In addition, I think it would be useful to be able to choose which column of the array you'd like to sort by.

Sort by column would be amazing! Then you could just reduce the size of the array, making a top 10 really easy.

I ended up coming up with my own solution, which works... but damn.. it's pretty complicating... still hoping for another solution which works better... but I like this idea.
B
47
S
15
G
3
Posts: 201
Reputation: 4,625

### » Wed May 07, 2014 8:40 pm

@Nitro187

Mine's pretty straight forward.

For yours, I would list it as:

1 1345 Matt Car 56 Green
2 2345 Chris Truck 93 Red
3 23255 Laura Train 15 Blue
4 92234 Ashley Bike 134 Yellow
5 7346 Sam Plane 99 Purple

Note the score first.

Then I include a 6th element in the array which is the new one, so for example:

6 1000000 AnD4D Plane 10 Green

Then I run the data sort command, which puts them into ascending order based on the first element in the array:

1 1345 Matt Car 56 Green
2 2345 Chris Truck 93 Red
3 7346 Sam Plane 99 Purple
4 23255 Laura Train 15 Blue
5 92234 Ashley Bike 134 Yellow
6 1000000 AnD4D Plane 10 Green

Sadly in the opposite order I want them, but then I delete the top array, and just load the data into the text file in the order I want them displayed.

That make sense? I know you already have your method, but there's no harm in sharing in case anyone else comes along to this thread.
B
97
S
20
G
8
Posts: 1,062
Reputation: 12,725

Return to How do I....?

### Who is online

Users browsing this forum: savvito123 and 11 guests