How do I filter one array into another array?

  • I have an array with a width of 440 and a height of 5. It looks like this:

      [score, text1, text2, category, id]

    For my app I need to sort the array by score but also filter it by whichever category the user chooses. To accomplish this I created another array and set it to have the same dimensions. Then I iterate through the array that has my data and copy ONLY the rows that match the chosen category into the new array. That part works great.

    Unfortunately, since both arrays have the same dimensions, my new filtered array isn't as big and so has a bunch of "0,0,0,0" lines at the end.

    How do I fix this? I suppose I could set all the "0" scores to a really high number so they would get sorted to the bottom, but there has to be a more elegant solution. Can I search through and delete the "0,0,0,0" rows?

    Not sure if it will be useful but here's a picture of my function:

    NOTE: On 'Start of layout' I load all my data into "array2" and this function copies everything that matches category "x" into the original array ("array")

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can set your second array to Size(0,5,1) and then do a Push on X, then set your values. This will only push as many as you need.

  • Thanks, blackhornet. I tried using push last night and failed. Came online to learn more and saw some posts that said its not really for multidimensional arrays, so I gave up on the idea. Will look at it some more tonight, but if anyone has any advice...

  • As long as you only Push on X, it works.

    http://www.blackhornettechnologies.com/ ... ample.capx

    Run this with the Debugger, and click on Array.

  • Now you stille have (probably) a sorting problem.

    Sorting in 2D and 3D arrays can not work as you expect it to work.

    Hope i give you ideas with this cap (avoiding a sort algoritme)

    drive.google.com/open ... ElHMHpIeVU

    Greetings.

  • id=0B1SSuCVV8v74N0JEUElHMHpIeVU

    (can not post links yet)

  • Thanks 99instances2go, I appreciate the help -- but I've never had any issues sorting my array. I only need to sort by the "score" and it's as simple as "sort x". Works perfectly.

    Oh, and thanks for the help with "push", blackhornet. With your help I got my app working last night. It has taken me almost a month (for something I thought I could do in a weekend), but it's almost finished now.

    Most of my Flashcard app was super simple to make using C2, but I struggled with array handling, loading/saving data, the dictionary object, and local storage. The next project should go a LOT smoother.

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