Sorting variables? (Rank System)

Hi, I'm trying to create a ranking system for my project.

NPCs fight each other when the player moves to another map or fights against another NPC.

I have several variables with the the current wins of each player.

Simple example:

Player 1:  12 wins  # $game_variables[1] == 12  -> Rank 3

Player 2:  15 wins  # $game_variables[2] == 15  -> Rank 2

Player 3:    2 wins  # $game_variables[3] == 2    -> Rank 5

Player 4:    5 wins  # $game_variables[4] == 5    -> Rank 4

Player 5:  21 wins  # $game_variables[5] == 21  -> Rank 1

Leaderboard:

Gold:     Player 5 with 21 wins

Silver:    Player 2 with 15 wins

Bronze: Player 1 with 12 wins

etc.

To give every player a rank and create a leaderboard, I would have to lay out a plethora of conditional branches.

I wonder if there's an easy way to do this?
The sorting part can be done easily using an array i guess.

You don't need  full script for that to work but you will need some script calls.

In an event script call, try this:

@ranking = [$game_variables[1], $game_variables[2], $game_variables[3], $game_variables[4]]@ranking.sort!$game_variables[6] = @ranking[0]$game_variables[7] = @ranking[1]$game_variables[8] = @ranking[2]$game_variables[9] = @ranking[3]This should give you the newly sorted numbers in Variables 6~9 (assuming you don't want to overwrite the old variables)

edit:

To get the sorting in reversed order, 

replace

@ranking.sort!with this

Code:
@ranking.sort!.reverse!
Try something like

ary=[$game_variables[1], and so on]rank={}ary.sort!(1..5).each do |i| (1..5).each do |x| if ary == $game_variables[x] rank = [x,ary] break endendNow you have a hash which saved which player is at each rank and what the score is... Showing it depends on how you want to actually show the leaderboard.This might not be the most efficient way (or might not work), I just wrote this out of my mind. XD

@meow - the problem with that is you dont have a link to which player has that score which I think he needs judging from his post.
Yup, leaving that out for flexibility, so he/she can pair it with the "players" in the leader board comparing the old variables with the new ones in his/her own way. (Don't want to end up making the whole leader board for him/her with my sleepy head. lol! 😉 )

Your way paired the array with a number might just work if he/she knows how to implement it. 🙂
The sorting MeowFace suggested, works perfectly fine. I can now compare the wins (if $game_variables[1] == $game_variables[6]) in a conditional branch and give everyone its rank. Sharing positions/ranks is no problem in my case.

To be honest Adiktuzmiko's way is too pro for me. I don't really know what to do with it. Haha!

Thank you both, your really helped me out!
Does anyone know how to do this in MV?


I tried different ways, but couldn't figure out the correct JS syntax.

Original thread: https://forums.rpgmakerweb.com/threads/sorting-variables-rank-system.44559/