How to pick a Variable with the highest amount?

I'll try to make this sound not so complicated of what I'd like to accomplish.

Let's say I have Variables 23 through 31.
At the end of the game, which ever variable is the highest, I'd like to display a text message depending on the highest one.

is it possible to event this or use script/code to accomplish this? (Not plugins. But if plugins are the only way, let me know)
Pick another variable and make a script call:

$gameVariables.setValue(X, Math.max(Y1, Y2, Y3, Y4, ...);

Replace X with the new variable, which will be the one used to display the message.
Replace the Y's with "$gameVariables.value(Z)"
-> Z will be the variable ID of each variable in your cycle, e.g. the 23 through 31.

So the script call would look like this for example: This is for the first 3 variables of your example.
You will of course need to go to 31.

$gameVariables.setValue(32, Math.max($gameVariables.value(23), $gameVariables.value(24), $gameVariables.value(25)));

^ After this script call Variable 32 is set to the highest number from variables 23 through 25.
you'll basically need a sorting function - there are sorting scripts in every programming handbook and in some cases they were even implemented as base functions.
You only need to adapt them to whatever data you want as the original functions usually work on arrays and not on simple variables.

If you don't need to sort you can also make this an event sequence where you simply compare every variable with the current max one in event code with conditional branches - tedious but doable
It's quite simple to write a find max routine for this:

JavaScript:
let max = -1, maxId;
for (let i = 23; i <= 31; i++) {
  let val = $gameVariables.value(i);
  if (val > max) {
    max = val;
    maxId = i;
  }
}
// At the end, max and maxId have the maximum value and 
// the respective variableId. Use them as you need.
// e.g., putting the max on another variable:
$gameVariables.setValue(32, max);

No need for sorting algorithms or manually typing in all the variables.
Pick another variable and make a script call:

$gameVariables.setValue(X, Math.max(Y1, Y2, Y3, Y4, ...);

Replace X with the new variable, which will be the one used to display the message.
Replace the Y's with "$gameVariables.value(Z)"
-> Z will be the variable ID of each variable in your cycle, e.g. the 23 through 31.

So the script call would look like this for example: This is for the first 3 variables of your example.
You will of course need to go to 31.

$gameVariables.setValue(32, Math.max($gameVariables.value(23), $gameVariables.value(24), $gameVariables.value(25)));

^ After this script call Variable 32 is set to the highest number from variables 23 through 25.

Thanks for the reply! I'm a bit confused on how exactly would I make the message depending on which one is highest?
@Morpheus Well you would just put in the usual text code like \V[32] for example.
Or you know, whatever variable you put in for X.
@Morpheus Well you would just put in the usual text code like \V[32] for example.
Or you know, whatever variable you put in for X.

Sorry, I meant, hypothetically speaking, let's say variable 24 is highest, then the message might say "you suck", but if 25 is highest, it might say "You tried."
let's say variable 24 is highest, then the message might say "you suck", but if 25 is highest, it might say "You tried."
That is not covered by any solution so far, because you never mentioned in the original post as to what you wanted to do with the result.

and basically the solution is to store the ID of the highest variable into another variable, and then make a series of conditional branches "if X=1 show text 1, if X=2 showtext 2,.." and so on.
@Morpheus In that case you would probably want to with @Andar 's suggestion.
I was not aware that you did not actually want to display the value of the variable itself.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-pick-a-variable-with-the-highest-amount.128265/