I'm trying to make a puzzle game in which you arrange blocks in a 6x6 grid by sending all blocks in a row or column towards a single direction at once. Basically, a recreation of this minigame from Mario & Luigi:
https://youtu.be/vYZpsaA66Ug?si=Y_UcktzKQjjV2mf3&t=74As suggested to me by another person on this forum, I created a working version of the game using a 2D list containing numbers representing each differently colored block or empty space. In order to give the game better visuals, I'm now trying to have actors visually represent the blocks as they move around the grid. I'm aware I could just make a single actor with animations corresponding to the numbers in the 2D list, which is what I've been doing to visualize the 2D list, but I opted for actors so that they could animate properly.
In order to make moving rows and columns faster and more consistent, I elected to have them snap to their desired position nearly instantly, unless their desired position is the same as their current one. To determine which actor is in which row or column, I add them to the appropriately numbered list within a list, one of two containing either rows or columns. Unlike the 2D list, these lists only contain actors, without anything representing the empty space between them.
As each block moves within a row, I have to remove them from their current column and add them to their new one, determined via an attribute that defines their position on the X axis within the grid. Because my code is dependent on going through each list in order from left to right (the code for moving blocks to the right simply works off of a temp list that's based on a reversed version of the desired row), I have to make sure to order the column the block made its new home based on each block's position on the Y axis. I created a custom block that is supposed to do this, but as I'm not experienced with sorting algorithms, I'm not sure if it's the source of the problem or not. Either way, something is going wrong.
Sometimes when I move the blocks in any direction, the blocks appear in the wrong order. Instead of a block that, say, includes three blue blocks sandwiched by two red blocks, one of the red blocks will swap places with the blue blocks in the middle. The number of blocks is always the same, so I know they're not overlapping each other. My best guess is that the sorting algorithm is placing the blocks in the wrong order, but I could also be failing to place them in the correct row/column before I even trigger the sorting custom block, or it could be my entire approach with movement that's flawed.
I've included all the relevant code below, as well as visual examples of the problem (the numbers on each actor represent the value of their Position Y attribute and are only being drawn for testing). It includes the custom blocks that determine movement from left to right, right to left, up to down, and down to up, triggered via another behavior, as well as the sorting algorithms. Pardon the redundant code, I was worried about breaking something I thought didn't need fixing.
Is the issue with my "Sort Column/Row Number ___" blocks or my "Move Actor Blocks in ___ Left/Up" blocks?
EDIT: After additional testing, I was able to confirm that the sorting function was not working correctly. I outputted the values of the actors' position in the list of columns, and they changed to 0, the beginning of the list, as opposed to where they were supposed to be relative to their position in the grid. I'll look into how to make a proper sorting algorithm once I can, but for now any help would be appreciated.
TL;DR: How do I sort actors from the lowest value of an attribute to the highest?