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

CloseRange

Member
oh god yes arrays would be so useful here.
the method I'll show you is the easiest but slowest way to sort numbers (slowest as in compile time) however beacuse this is only 15 numbers you really don't have to worry one bit.
this is mostly a copy and paste script (hense why arrays would be better) so we can go fancy and make 2 scripts instead of 1. call the first 1 "checkValues":
Code:
/// checkValues(value1, value2);
if(argument[0] < argument[1]) {
     return false;
}
return true;
sadly we cant directly change the variables passed in or this would be so much easier.
Make the second script "sortOrders":

Code:
/// sortOrders();
var sorted = true;
if(!checkValues(order1_x, order2_x) {
     var t = order1_x;
     order1_x = order2_x;
     order2_x = t;
     sorted = false;
}
if(!checkValues(order2_x, order3_x) {
     var t = order2_x;
     order2_x = order3_x;
     order3_x = t;
     sorted = false;
}
if(!checkValues(order3_x, order4_x) {
     var t = order3_x;
     order3_x = order4_x;
     order4_x = t;
     sorted = false;
}
.
.
.
.
// at the bottom
if(!sorted) sortOrders();
if this was an array you could just use a for loop instead.

btw this uses recursion, by recalling the same script over and over again until everything is sorted.
Don't worry about calling this every step.
If everything is already sorted it will run faster. but if everything is out of order (probobly right at the start) that's when it runs the slowest
 
7

7ynal

Guest
There are a few ways you can do it. One is a brute force way, similar to bubble sort. You can compare say order1 >= order2, if true you would swap the values. You repeat this comparing order1 to all other orders. That gives you the smallest value in order1. However, you have to repeat this for each one: order2 >= order3 ...order2 >= order3...order5 >= order6...on and on. It is not efficient but it will give the 'order' you want. Another option is to shove all the values into an array. Sort the array however you wish. Then assign the orders to their related index: order1 = array[0]; order2 = array[1];

If this doesn't help let me know. I can give you more detailed examples here in a bit.
 
oh god yes arrays would be so useful here.
the method I'll show you is the easiest but slowest way to sort numbers (slowest as in compile time) however beacuse this is only 15 numbers you really don't have to worry one bit.
this is mostly a copy and paste script (hense why arrays would be better) so we can go fancy and make 2 scripts instead of 1. call the first 1 "checkValues":
Code:
/// checkValues(value1, value2);
if(argument[0] < argument[1]) {
     return false;
}
return true;
sadly we cant directly change the variables passed in or this would be so much easier.
Make the second script "sortOrders":

Code:
/// sortOrders();
var sorted = true;
if(!checkValues(order1_x, order2_x) {
     var t = order1_x;
     order1_x = order2_x;
     order2_x = t;
     sorted = false;
}
if(!checkValues(order2_x, order3_x) {
     var t = order2_x;
     order2_x = order3_x;
     order3_x = t;
     sorted = false;
}
if(!checkValues(order3_x, order4_x) {
     var t = order3_x;
     order3_x = order4_x;
     order4_x = t;
     sorted = false;
}
.
.
.
.
// at the bottom
if(!sorted) sortOrders();
if this was an array you could just use a for loop instead.

btw this uses recursion, by recalling the same script over and over again until everything is sorted.
Don't worry about calling this every step.
If everything is already sorted it will run faster. but if everything is out of order (probobly right at the start) that's when it runs the slowest
You can avoid the recursion by using a while loop instead but it's only one level recursion so it's no biggie.
There are a few ways you can do it. One is a brute force way, similar to bubble sort. You can compare say order1 >= order2, if true you would swap the values. You repeat this comparing order1 to all other orders. That gives you the smallest value in order1. However, you have to repeat this for each one: order2 >= order3 ...order2 >= order3...order5 >= order6...on and on. It is not efficient but it will give the 'order' you want. Another option is to shove all the values into an array. Sort the array however you wish. Then assign the orders to their related index: order1 = array[0]; order2 = array[1];

If this doesn't help let me know. I can give you more detailed examples here in a bit.
Thanks for all the suggestions, I'm going to try these a get back to you guys. Efficiency is key right now since the values need to be ordered within a few frames, but since its only 15 values (most of which will be 0) I think some these methods will work.
 

CloseRange

Member
Thanks for all the suggestions, I'm going to try these a get back to you guys. Efficiency is key right now since the values need to be ordered within a few frames, but since its only 15 values (most of which will be 0) I think some these methods will work.
every one of these methods work within 1 frame. However if it becomes bad then a frame could extend beyond the normal 1/30th of a second.
It would be such a small decrease that I doubt you could even measure it because of how small this is. Were talking micro efficiency here that normally doesn't matter until you need to sort hundreds of values.
 
D

dannyjenn

Guest
Why not load them into a ds_list, call ds_list_sort(), then load them back into their variables? I'm not sure what sort of algorithm ds_list_sort() uses, but it's probably pretty fast.
 
Why not load them into a ds_list, call ds_list_sort(), then load them back into their variables? I'm not sure what sort of algorithm ds_list_sort() uses, but it's probably pretty fast.
Yeah, I really should have done that from the beginning but we figured it out now so all is well.

every one of these methods work within 1 frame. However if it becomes bad then a frame could extend beyond the normal 1/30th of a second.
It would be such a small decrease that I doubt you could even measure it because of how small this is. Were talking micro efficiency here that normally doesn't matter until you need to sort hundreds of values.
There are a few ways you can do it. One is a brute force way, similar to bubble sort. You can compare say order1 >= order2, if true you would swap the values. You repeat this comparing order1 to all other orders. That gives you the smallest value in order1. However, you have to repeat this for each one: order2 >= order3 ...order2 >= order3...order5 >= order6...on and on. It is not efficient but it will give the 'order' you want. Another option is to shove all the values into an array. Sort the array however you wish. Then assign the orders to their related index: order1 = array[0]; order2 = array[1];

If this doesn't help let me know. I can give you more detailed examples here in a bit.
Everything seems to be working, I'll have to do some more testing to be 100% sure but I doubt I'll have any issues. Thanks for all the help
 
Top