Legacy GM All I want to do is sort an array by value ..Please help

Okay, i'm working on a game and all I want to do is sort ten valued items quickly.

so I already have ten items in my game sorted in the variable Item_Name
Item_Name[1] = "Jeans"
Item_Name[2] = "Shirt"
Item_Name[3] = "Wool Gloves"

etc up thru 10 items

and then I already have ten values that change but Item_Cost reflects the cost of each item...
Item_Cost[1] = 55;
Item_Cost[1] = 40;
Item_Cost[3] = 25;

etc up thru 10...

Obviously Item_Cost[1] is the cost of the Item_Name[1} (Jeans), Item_Cost[2] refers to the cost of the shirt, 3 the cost of the wool gloves.

Because these costs go up and down based on supply and demand in my game, which I've already got working.. now all I want to do is be able to list these items by their cost with the top item being the most expensive item at the time, Item 2 on the list being the 2nd most expensive, etc.

I understand I can do this with an array and making and deleting a DS list and sorting it but I can't get it to work.
I found this array sorting script: https://www.gmlscripts.com/script/array_sort

But I keep getting an error saying argument 2 incorrect type (array), expecting a number.

Here's how I have that same script in my code:

var array = Item_Cost;
var ascend = Item_Name;
var list = ds_list_create();
var count = array_length_1d(array);
for (var i=0; i<count; i++) list[| i] = array;
ds_list_sort(list, ascend);
for (i=0; i<count; i++) array = list[| i];
ds_list_destroy(list);
return array;

I feel dumb but I guess that I've never really grasped arrays at all. I'm sure it's simple. If someone could please show me in code how I can do what I want to do and draw these item names and their values which change often in my game... I'd greatly appreciate it and I think it would also help me understand arrays better.
 

Le Don

Member
The part after your first for loop is wrong. As I see it, list is a list and array an array, which means you have to use something like
ds_list_add(list, array[ i ])

and after the second loop it's similar wrong. You're using [.] at the wrong object.

Btw. You don't have to use an arrays, convert it to a list and back to an array. You can also use a grid, which is like an array, but with way more functionality and also a sort function. It's similar to a list, but in 2D.
 
Last edited:

Tyg

Member
a suggestion
firstly i would keep the array together as one
then you do a bubble sort on the cost
ill see if i can whip up some code :)
 
still hoping someone can give me some code to work with... I don't have GM2. I have GM 1.4999
 
Last edited:

Nidoking

Member
array is an array. It is an entire array. list[| i]=array is assigning the entire array into a list element. You don't want the whole array in a list element. You want the i'th element in the array. Just like list[| i] is the i'th element in the list, array[ i ] is the i'th element of the array.
 

TsukaYuriko

☄️
Forum Staff
Moderator
ds_grids have built in sorting by columns... my advice would be to use that instead of re-inventing the wheel. ;)
 

Le Don

Member
Dude, I basically gave you a line of code and told you where to use it and where the next problem is. Another one pointed out the same stuff. And now you have two people mentiong grids. Check out the docu about arrays and lists (and better: grids) - not only is the stuff explained very well, they also use examples.

I know you don't want to heart his stuff. But at some point you have to use your own legs to learn how to walk.
 

FrostyCat

Redemption Seeker
See: ds_grid_sort
GML:
var n = array_length_1d(Item_Name);
var sortGrid = ds_grid_create(2, n);
for (var i = n-1; i >= 0; --i) {
    sortGrid[# 0, i] = Item_Name[i];
    sortGrid[# 1, i] = Item_Cost[i];
}
ds_grid_sort(sortGrid, 1, false);
for (var i = n-1; i >= 0; --i) {
    Item_Name[i] = sortGrid[# 0, i];
    Item_Cost[i] = sortGrid[# 1, i];
}
ds_grid_destroy(sortGrid);
 
See: ds_grid_sort
GML:
var n = array_length_1d(Item_Name);
var sortGrid = ds_grid_create(2, n);
for (var i = n-1; i >= 0; --i) {
    sortGrid[# 0, i] = Item_Name[i];
    sortGrid[# 1, i] = Item_Cost[i];
}
ds_grid_sort(sortGrid, 1, false);
for (var i = n-1; i >= 0; --i) {
    Item_Name[i] = sortGrid[# 0, i];
    Item_Cost[i] = sortGrid[# 1, i];
}
ds_grid_destroy(sortGrid);
FrostyCat! You the bomb! Thank you! I was having a big problem, didn't understand or see that I needed to set the array length and it kept giving me errors. You are my hero <3 Thanks to everyone else as well!
 
Top