Putting numbers in order

How can I make a formula to find the 2nd or 3rd highest number of a group of numbers?
It depends on whether your group of numbers contains all unique numbers or whether some can be repeated, and if they can be repeated, if you ask for the 'second highest number' and your list is 7 7 7 6 5 4, is it the 7 (second number) or the 6 (second highest number) that you want returned?


How are you using this - are you writing a script, or making an event?  What is your list of numbers - a group of variables, an array (and how is it defined)?
I want it for a damage calculation, and it will get 5 random numbers and then based on another factor it'll pick which number.


Yes, numbers can be repeated, and if 2 7's came up i want the 2nd highest to be 7.
Do you want the formula to provide the 5 random numbers, or will you provide them?  If the formula is to provide them, what range should they be in?  If you're providing them, what is the name of the object/array that will contain the numbers?


Ultimately, you'll need to sort the array, then choose the second last number (if it's sorted in ascending order):

Code:
arrayname.sort()[arrayname.length - 2]

but arrayname needs to be an existing array, so the above assumes it's already been created and populated.
I don't have anything set up for it yet, so I can make it either way. The number range will be based on the attack power, weapon, and skill used.


It will probably be best to make a function with the array ordering code that I can call from the dmg calculation window.


How would I call a function in the dmg calc window to pull out the number I want?  Its the JS that I don't know.

Original thread: https://forums.rpgmakerweb.com/threads/putting-numbers-in-order.63237/