How do I sort on array with several matching Y values?

0 favourites
  • 3 posts
From the Asset Store
Match 3 Game Gem Pixel Art work for any kind of Match Games.
  • If I have a two dimensional array like this:

    0,1

    0,2

    0,3

    0,4

    0,5

    0,6

    0,7

    0,8

    0,9

    0,10

    and then I sort x, I get this:

    That's unfortunate, as I would like Y1 to still be ordered. I know the sort is just looking at Y0, but If the sort encounters 10 or so of the same value in Y0, then the Y1 sorting will be weird like above (it seems to swap X0 with the X row in the middle of whatever array I use).

    If I use this method of sorting multiple columns (to sort by Y0 then Y1), I get the same result. The methods I can find for sorting by multiple columns don't seem to work if there are too many matching values in Y.

    The array I'm working with is actually about 200x14 size (using the CSV to Array plugin) but I can reproduce the dillema using the above, simpler example. In my array I'm using Y0 as an active flag/bit. I need to reverse sort by the active flag and then secondarily sort by another Y column (which denotes a category). Should I bypass the standard sort method in favor of another logic? I think I'm about to abandon the idea of sorting by active flag and just have the program traverse the active row scattered throughout the array.. and sort only one column using a swap method. It's just so much easier to debug when its sorted. Thanks for any ideas! -Steve

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If this is just for debugging, I would make a clone of this array (CloneArray load from JSON MainArray.AsJSON) and then simply remove rows from this clone array. Say, press one button on the keyboard, see only records with Y0=0, press another button - see records with Y0=1. You can then sort by some other column if you need.

  • Thanks! I'll look into that. The more I look at it, the code also is somewhat dependent on the array being sorted in a way where active/relevant rows are contiguous. I'd rather not take the testing hit on changing the logic to deal with an unsorted array but may have to. Maybe cloning will also play a role there. It's interesting, how the sorting behaves.

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