JS Script Noob Question about Adding, Setting, and Sorting items in an array

I'm looking to add a script call that can take a list of existing game variables and sort them in order from highest to lowest. I'm not too familiar with how to do this in RPG Maker MV. Here's my noob attempt, but it's obviously wrong:

Code:
var numbers = [$gameVariables.value(33),$gameVariables.value(37),$gameVariables.value(41),$gameVariables.value(152);
numbers.sort();
$gameVariables.setValue(152) = numbers[0];
$gameVariables.setValue(153) = numbers[1];
$gameVariables.setValue(154) = numbers[2];
$gameVariables.setValue(155) = numbers[3];

I'm curious how I might actually do this properly so that I can save out the ordered values from the array into new variables.

Thanks!

-~-

I've moved this thread to Learning JS. Thank you.

Sorry about that. Thanks!
Try this:
Code:
numbers.sort((a, b) => b - a);
I get the following error:

1592079111785.png

1592079140262.png


This is what the script looks like in the event. Perhaps I am formatting it wrong?

1592079200536.png
=> is a separtor for 1.6.x, so my suggestion is that you use 1.5.x?
I dont know how to revert it though, but the code is slightly different.

if you use 1.6.x, it should work.
Well you're using $gameVariables.setValue() wrong. It should look like this:

Code:
$gameVariables.setValue(varId, newValue)

The error you're posting means a ; was encounter that doesn't make sense, or possibly that something else is broken/done wrong. Or...

Wait, what version of RMMV are you using?

Edit: I'll just write a version that'll work with older versions of RMMV:

Code:
numbers.sort((function(a, b){ return b - a; }));
=> is a separtor for 1.6.x, so my suggestion is that you use 1.5.x?
I dont know how to revert it though, but the code is slightly different.

if you use 1.6.x, it should work.

Sorry, I'm not too familiar with the terms used. What does 1.6.x and 1.5.x refer to? A version number? Or is there something in the system that I want to change to make the above script work?

Would you mind showing a quick example of the before and after state of what I would change in the above case?

Thanks

-~-
Well you're using $gameVariables.setValue() wrong. It should look like this:

Code:
$gameVariables.setValue(varId, newValue)

The error you're posting means a ; was encounter that doesn't make sense, or possibly that something else is broken/done wrong. Or...

Wait, what version of RMMV are you using?

Edit: I'll just write a version that'll work with older versions of RMMV:

Code:
numbers.sort((function(a, b){ return b - a; }));
Thanks, I'll give this a try. i'm using 1.6.1 it appears, if that matters at all.

-~-
Thanks, I'll give this a try. i'm using 1.6.1 it appears, if that matters at all.

-~-
You might have something else going on, then. I'm using 1.6.2, and if I recall, there's no real difference between the two other than to keep our version caught up with Japan's, or something like that. If the error persists, could you post the topmost line in your script tag? The really long one with var numbers = [...]?
Well you're using $gameVariables.setValue() wrong. It should look like this:

Code:
$gameVariables.setValue(varId, newValue)

The error you're posting means a ; was encounter that doesn't make sense, or possibly that something else is broken/done wrong. Or...

Wait, what version of RMMV are you using?

Edit: I'll just write a version that'll work with older versions of RMMV:

Code:
numbers.sort((function(a, b){ return b - a; }));

So I no longer get an error when I run the game, so that's good. However, none of the values appear to get stored in the desired variables. Here's what the latest looks like after your suggestions:


Code:
var numbers = [$gameVariables.value(33),$gameVariables.value(37),$gameVariables.value(41),$gameVariables.value(152);
numbers.sort((function(a, b){ return b - a;}));
$gameVariables.setValue(152, numbers[0]);
$gameVariables.setValue(153, numbers[1]);
$gameVariables.setValue(154, numbers[2]);
$gameVariables.setValue(155, numbers[3]);

See any issue with why the variables 152-155 would not be set to the values in the numbers array? Is there some special syntax or formatting that they require as well?

Thanks,

-~-
Hmm, actually scratch that. The error does still occur. It just didn't trigger due to the script blob being disabled by a debug switch I had. So I still have the original problem.

Hopefully you can see the first full line of the script above for reference as you asked. Just in case, here it is again:

Code:
var numbers = [$gameVariables.value(33),$gameVariables.value(37),$gameVariables.value(41),$gameVariables.value(152);

And here's the current error:

1592084439800.png
Also, for the compare function, is this the proper format?

1592084996148.png


I noticed the one you sent had a few additional parenthesis. Is that a requirement for RPG Maker MV?

-~-
Hi there!
I believe the problem is that you are not closing the array, you are missing the last bracket ']'.
var numbers =[$gameVariables.value(33), $gameVariables.value(37), $gameVariables.value(41), $gameVariables.value(152);] - Remove the ";" and add ] to the end, to close the array.

I think your script should look like this:
JavaScript:
var numbers = [$gameVariables.value(33),$gameVariables.value(37),
$gameVariables.value(41),$gameVariables.value(152)];
numbers.sort((a, b) => b - a);
$gameVariables.setValue(152) = numbers[0];
$gameVariables.setValue(153) = numbers[1];
$gameVariables.setValue(154) = numbers[2];
$gameVariables.setValue(155) = numbers[3];

Do you want to sort the values? Or the variable id's inside the array?
Hi there!
I believe the problem is that you are not closing the array, you are missing the last bracket ']'.
var numbers =[$gameVariables.value(33), $gameVariables.value(37), $gameVariables.value(41), $gameVariables.value(152);] - Remove the ";" and add ] to the end, to close the array.

I think your script should look like this:
JavaScript:
var numbers = [$gameVariables.value(33),$gameVariables.value(37),
$gameVariables.value(41),$gameVariables.value(152)];
numbers.sort((a, b) => b - a);
$gameVariables.setValue(152) = numbers[0];
$gameVariables.setValue(153) = numbers[1];
$gameVariables.setValue(154) = numbers[2];
$gameVariables.setValue(155) = numbers[3];

Do you want to sort the values? Or the variable id's inside the array?
Oh thanks for that! I'm looking to sort the values of those variables.

-~-
And that solved the problem. Thanks a bunch!

Original thread: https://forums.rpgmakerweb.com/threads/js-script-noob-question-about-adding-setting-and-sorting-items-in-an-array.122825/