# Sort arrays based on each column or row

Get help using Construct 2

### » Sun Jun 25, 2017 9:12 am

Maybe I am missing something obvious here, but although I can do it by using multiple arrays, wondering if there is a solution that would let me use a single multidimensional array for this: In flash it is very easy, in construct I can apparently only sort X, Y and Z, In flash I can sort it any way I want, by specific column in Y for example.

Anyway this is what it would look like in Flash using multidimensional arrays:

Create array like this for each team : Array.push({name:teamA, rank:6, score:0, power:6, win:0, dr:0, lose:0, pt:0, gfor:0, gagainst:0, gd:0, posic:0});
In Flash I can sort based on any of those, name, rank, power, score, goals, etc.....

Then with code below do what I need to do. Sort by total points, and if points are equal between teams, sort those by goal differential, so teams with better goal differential come on top. With a single array in Construct 2 simple enough to sort team standings by total points, but not by goal differential because it will not sort points and goal differential values independently ( both stored in Y columns )

for (i = 0; i < 8; i++)
{
Array[i].posic = 0;
for (j = 0; j < 8; j++)
{
if (Array[i].pt < Array[j].pt and i != j)
{
Array[i].posic -= 1;
}
if (Array[i].pt == Array[j].pt and i != j)
{
if (Array[i].gfor - Array[i].gagainst < Array[j].gfor - Array[j].gagainst and i != j)
{
Array[i].posic -= 1;
}
}
}
}
B
36
S
19
G
7
Posts: 142
Reputation: 6,886

### » Fri Aug 11, 2017 1:33 am

Only solution I've found is to just add a new column at the front, copy the values from the column I actually want and then sort before popping the front.
I'd love to know if there's a better way.
B
22
S
9
G
1
Posts: 43
Reputation: 2,156

### » Fri Aug 11, 2017 11:04 am

Since ....

1/ C2 is marketed as "You can now make advanced games without writing a line of code. Construct 2 does the hard work so you don't have to."
2/ I belong to the targeted category of 'scripters'.
3/ I can only trust (because of point 1) that i can never ever reach the performance of the build in Array sort algorithm. And (because of point 2) i am not supposed to have the knowledge to beat that performance.

....... i use this lazy method. Basically it comes down to reserving the X-axis for sorting by self-made 'filters', and store every value on the Y-axis.
https://www.dropbox.com/s/brvmx9pupif37 ... .capx?dl=0
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Sun Aug 13, 2017 3:48 am

Thanks, I will check it out. I will need it soon. I kind of stopped and concentrated on other stuff ( always seems like working on at least 4-5 games at the same time ).

I think with construct it's a matter of wrapping one's head around its way of working. 90% of the time it is easy, but the other 10% seems like it would be easier if you could just script it the old fashioned way ( which may not be true, but it appears that way to me ).
B
36
S
19
G
7
Posts: 142
Reputation: 6,886