# How do I move information around in arrays...or maybe it's

Get help using Construct 2

### » Wed Apr 26, 2017 2:36 am

What I want to do is have a value, let’s say “Name” and attached to that value is another set of values, say “Score” and “Address”. Now I want to arrange the “Names” by the value “Score”, but I want the other values (“Name”, “Address”, etc.) to stick with that “Score” value such that I may be able to access “Address” in the order that the “Scores” are ranked.

How would I do that?

I’ve reviewed plenty about arrays, sorting, multiple dimension arrays, etc., and I think the solution is right there in front of me, but my brain begins to haze up - am I am overthinking it.

For example: If there is a two dimensional array where the Y dimension is “Names” and the X dimension is “Scores”, and I sort by the X dimension (“Scores”) will the Y dimension (“Names”) move with the X dimension (“Names”)? In other words, if I have a particular Score at position (3,1) and its corresponding Name is at position (3,2), and if I sort and the Score goes to position (4, 1) will the Name attached to that “Score” automatically go to position (4,2)?

Thanks!
Last edited by jhjconstruct on Wed Apr 26, 2017 2:58 am, edited 1 time in total.
B
31
S
12
G
2
Posts: 49
Reputation: 3,177

### » Wed Apr 26, 2017 2:57 am

that's sorting..

C2 doesn't have very powerful built-in sorting.. but if you make your first column "Names" you can sort by X axis and it will do the trick. And it will be ascending..

Any other kind of sorting then you probably have to do it manually yourself..
B
103
S
35
G
16
Posts: 1,224
Reputation: 17,192

### » Wed Apr 26, 2017 3:00 am

Ahhh...Jobel, you responded to quick. LOL. I just added edited my original question with an example - could you review and respond? Thanks much!
B
31
S
12
G
2
Posts: 49
Reputation: 3,177

### » Wed Apr 26, 2017 5:43 am

you can just run the debugger and it should show the array data if you are curious... but yes it will bring the whole "row" when sorting.

generally the way I think of arrays are like Excel spreadsheets

column A is the X axis AND the row number

once you include additional columns you need to get specific and into the Y.. think of them as coordinates.

and the X is always the Row number

so this has 2 columns and 3 rows:

X[0] Y[0] "John"
X[0] Y[1] 3231

X[1] Y[0] "Fred"
X[1] Y[1] 1291

X[2] Y[0] "Mary"
X[2] Y[1] 3920
B
103
S
35
G
16
Posts: 1,224
Reputation: 17,192

### » Wed Apr 26, 2017 7:28 am

Thanks Jobel!
B
31
S
12
G
2
Posts: 49
Reputation: 3,177

### » Wed Apr 26, 2017 3:58 pm

For advanced sorting of arrays (and shuffling), you generally want to use a second placeholder array. Push your values in the desired order to the second array, delete the index of the original values in the first array, and then optionally copy all the sorted values back to the original array.
B
59
S
29
G
122
Posts: 1,826
Reputation: 69,054

### » Thu Apr 27, 2017 1:23 am

Oosyrag, thanks for the validation of my psuedo-self-discovered mini-solution - I did something similar utilizing a global variable, but I can see how that would work on a larger level. Would you happen to know any examples, capx or the like that I can borrow from and modify for our project?
B
31
S
12
G
2
Posts: 49
Reputation: 3,177

### » Thu Apr 27, 2017 4:09 am

@jhjconstruct

I do a lot of database work with C2. I typically use 3 arrays: one to hold the data, a second one to filter the data, and the third to sort the filtered data.

The filter array only needs one column, which is the row number of the main array for any rows matching the filter criteria. The index array has two columns - the first is for the data being sorted, and the second is the line number to the filter array (which is just a link to the main array). basically you copy the column you want to sort from the main data array into column 0 of the index array, (and the row number it came from in column 1). Then use C2's array sort. This works great for databases with up to thousands of records. After that, you would want to send queries to your database, and let its engine do the work...

I made a quick sample, just using a main array and an index array. first it loads some data, then sorts the main array (in descending order). Then it builds the index with a primary and secondary sort field.

You will see that I often use constant variables for the field names - this makes reading the code much easier a few weeks / months later when you are trying to figure out what is going on...

you can get my sample here: http://www.rieperts.com/games/forum/SortArray.capx

and only want to sort by score, then you do not need to make an index, just sort the array. The name and address will stay with the corresponding score.
B
99
S
35
G
20
Posts: 438
Reputation: 17,950

### » Thu Apr 27, 2017 12:46 pm

@jhjconstruct, @oosyrag, @jobel, @AllanR

Here's a way to shuffle an array using only a single array and a local variable:

https://www.dropbox.com/s/h8pmfmjgfkvu8 ... .capx?dl=0

Run it in debug to check the array sorting.

It works by picking a row from within the array width, pushing it to the back of the array, and then using the loopindex to reduce the number of rows to pick from, until all rows in the initial array have been picked and pushed to the back.

I've just randomly shuffled, but if you wanted to apply a particular sorting criteria then you could modify event 3.
B
24
S
9
G
4
Posts: 515
Reputation: 4,472

### » Fri Apr 28, 2017 6:47 pm

AllanR and MekonBekon,

Thank you for the feedback and capx. I haven't tried them yet, as I kind of did a functional, but not the most lean, workaround. But, when I get the demo done, I'll be able to go back and tighten it up.

I really appreciate the time and effort you all took to help me out. Have a good weekend!

Jefferson
B
31
S
12
G
2
Posts: 49
Reputation: 3,177