• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Sorting a list without using arrays

So I am in a interesting predicament.

I have 15 variables called "order1_x" and the 1 is replaced by every number up to 15. These variables can hold a value from .1 to 30 and the numbers are always a maximum to the first decimal place. I have used these variables all over my code and its a bit to late to change everything to change the variables into an array (my fault I didn't do that in the first place).

I need to order the variables in a list from least to greatest, but keep the variables in order.

Example
Code:
order1_x == 10.4;
order2_x == 2;
order3_x == 4.5;

//I need code to make it.

order1_x == 2;
order2_x == 4.5;
order3_x == 10.4;
I really wish I made it an array from the start, but it would be way harder to change everything then just figuring this out.

Any help is appreciated, Thanks!
 
I would recommend using ds_list for sorting values:
Code:
my_list = ds_list_create();

// Add the values to the list
ds_list_add(my_list, order1_x, order2_x, order3_x);

// Sort the list
ds_list_sort(my_list, true);
Make sure you destroy the ds_list when you don't need it anymore or you will get memory leaks.
Code:
ds_list_destroy(my_list);
 

TsukaYuriko

☄️
Forum Staff
Moderator
I really wish I made it an array from the start, but it would be way harder to change everything then just figuring this out.
I wouldn't be so sure about that. When you build a workaround around messy code, the mess grows exponentially... invariably leading to near-unmaintainable code. I can't possibly provide advice how to do that and then claim that I "helped" without also providing a solution to the core issue.

This doesn't answer the original question, but I'm a problem solver, not a question answerer, so... maybe try using a project-wide regex search and replace in an external text editor like Notepad++? This would let you replace all occurrences of your variables with corresponding list variants without you manually having to open every file, looking for occurrences and replacing. On top of resolving the original issue of not using an iterable structure, this seems a multitude of times easier than attempting to write a sorting algorithm that operates on a non-iterable source or creating temporary lists every time you need to sort something.

As this answer may or may not be to your liking, here's some food for thought:
Code:
#macro order1_x order_x[| 0]
By using the list accessor, these macros will be valid both for reading the values and writing to them. If you add just two more lines (creating and destroying the list) to your project aside from the 15 macro definitions, this will essentially let you use a list as the technical back-end while retaining your current code, which you can continue to use as you have always been. You don't have to change your code and you're now using a list that you can sort.
 
Top