How do I sort a bidimensional array by one column [Solved]

Get help using Construct 2

Post » Mon Feb 06, 2017 7:10 pm

Hi everyone!

I'm trying to sort a kind of "big" C2 bidimensional array with 6 x cells and dynamic y cells like:

150, cell01, cell02, cell03, cell04, cell05
50, cell11, cell12, cell13, cell14, cell15
300, cell21, cell22, cell23, cell24, cell25
...

I want to sort it ascending by the first number of each row like:

50, cell11, cell12, cell13, cell14, cell15
150, cell01, cell02, cell03, cell04, cell05
300, cell21, cell22, cell23, cell24, cell25
...

Can someone brings me some help? Thanks :D
Last edited by Josek5494 on Tue Feb 07, 2017 12:14 pm, edited 1 time in total.
B
19
S
6
G
2
Posts: 258
Reputation: 2,644

Post » Mon Feb 06, 2017 7:30 pm

If you use the 'Sort' action on the array, it will sort by whatever is in the first column - so you should be able to go ahead and use 'Sort' :)
The LEVEL GENERATOR TOOLKIT



24 functions dedicated to producing randomly generated terrain for all kinds of games.
B
25
S
7
Posts: 242
Reputation: 2,306

Post » Mon Feb 06, 2017 7:37 pm

mrtumbles wrote:If you use the 'Sort' action on the array, it will sort by whatever is in the first column - so you should be able to go ahead and use 'Sort' :)


I tried it, selecting Y, but it sorts all the columns separately for me and takes all the information on the rows out of it place...
B
19
S
6
G
2
Posts: 258
Reputation: 2,644

Post » Mon Feb 06, 2017 9:16 pm

Uh, no. You want to sort on X. Y is kinda 'attached' to X. As branches on a tree. Why i visualise an array as a tree. Sorting on the Y will indeed rearrange the whole tree, forgetting the branches.

I would Use an temp array. (in dont know better)

For the first number (150 in '150, cell01, cell02, cell03, cell04, cell05') you can just sort on the X-axis.

Then you got to loop each Y on an each X index, copy the values on that Y index to an X-axis in a temp array.
Sort that array, and copy them back.

So copy 'cell01, cell02, cell03, cell04, cell05' to a temp array. Sort the Temp on X. Copy the sorted values back.

If you need an example, give me an array in a .capx. Will do.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Tue Feb 07, 2017 12:14 pm

Solved already! Thanks!
B
19
S
6
G
2
Posts: 258
Reputation: 2,644

Post » Wed Apr 26, 2017 4:11 am

Josek what was your solution?
B
24
S
10
G
1
Posts: 43
Reputation: 2,288


Return to How do I....?

Who is online

Users browsing this forum: David181, OBLIVION and 59 guests