0 Favourites

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

  • 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!

  • 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..

  • Ahhh...Jobel, you responded to quick. LOL. I just added edited my original question with an example - could you review and respond? Thanks much!

  • 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

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Thanks Jobel!

  • 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.

  • 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?

  • 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

    for your original question, if you just have columns: score, name, address in your array

    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.

  • jhjconstruct, oosyrag, jobel, AllanR

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

    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.

  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)