Has anyone ever made a code that ranks variables?

There is a way I did mine but I was only limited to 4 variables like this

Rank Name Points

  1.    Ravens       30
    
  2.    Hawks      14
    
  3.    Terzers   9
    
  4.     Gierns     5
    

So the method I used to code ,can accumlate more than 4 variables ${name} but there will be too much coding.
I was wondering if someone made a code so one can customize their leaderboard without any heavy coding?

Can you explain better what you need and how you plan on using this rank? And you can post what you have tried so it’s easier to give you pointers.


In any case, I think you can achieve what you want by using a simple sorting algorithm, such as Bubble Sort, and strings to emulate arrays. There are a few solutions on this forum for how to emulate arrays with strings. It should be a little bit of a challenge but not unfeasible.

1 Like

If you have watched football, or played Dream league ,that table that shows teams and their points is similar to what I’m trying to describe.
On the table you won’t find a team with 3 points on number 1 if it has the least points.

I would look into Bubble Sort to see if it has what I am looking for

1 Like

I can think of a way to do it, but it’d be super messy. (I’m just doing 3 because I’m lazy and it ups the number of code lines but can add the other in if you need.)

Where raven, terzers and hawk are variables that have the numerical scores:

*comment first place
*if (raven > hawk) and (raven > terzers)
   *set firstvalue raven
   *set first "Raven" 
*if (terzers > hawk) and (raven < terzers)
   *set firstvalue terzers
   *set first "Terzers"
*if (raven < hawk) and (hawk > terzers)
   *set firstvalue hawk
   *set first "Hawk"

*comment second place
*if (first = "Raven")
   *if (terzers > hawk) 
      *set secondvalue terzers 
      *set second "Terzers"
   *if (hawk > terzers)
      *set secondvalue hawk 
      *set second "Hawk"

(Do the same for *if first = Hawk and Terzers)

*comment third place

*if (first = "Raven") and (second = "Hawk")
   *set thirdvalue terzers
   *set third "Terzers"
*if (second = "Raven") and (first = "Hawk")
   *set thirdvalue terzers
   *set third "Terzers"

(Do the same for the other 2 variables.)

*comment ranking

Rank Name Points

1. ${first} ${firstvalue}

2. ${second} ${secondvalue}

3. ${third} ${thirdvalue}
1 Like

Thanks ,I had something done like that,though mine didn’t have subsitution method in your example.That might help

1 Like

Hope that helps :slight_smile: I’ve used something like this before and it does work but does get a bit messy the more places you have to account for.

Btw sorry I had a few variables around the wrong way for the first place setting, just changed them.

1 Like

This might still end up being too much code depending on the number of teams you’re planning to have, but this is what I came up with. I added comments with “TO ADD MORE” in the place where you would add code to account for more teams. Hope it helps!

Code hidden
*comment create teams + scores, and ranks with pre-assigned team
*temp ravens      1
*temp hawks       5
*temp terzers     12
*temp gierns      3
*temp TEAM_5      100
*comment TO ADD MORE: *temp TEAM_X 0
*temp rank_1      "ravens"
*temp rank_2      "hawks"
*temp rank_3      "terzers"
*temp rank_4      "gierns"
*temp rank_5      "TEAM_5"	
*comment TO ADD MORE: *temp rank_x "TEAM_X"	
*comment ::----------------------------::		
*comment this is the bit that you actually see:
*comment first show the default unordered leaderboard, then show the ordered on
[b]Unordered[/b]
*gosub displayLeaderBoard 5
*gosub orderTeams 
[b]Ordered[/b]
*gosub displayLeaderBoard 5
*page_break
*comment then a test to make sure it works alright by randomising the scores
*rand ravens      0 100
*rand hawks       0 100
*rand terzers     0 100
*rand gierns      0 100
*rand TEAM_5      0 100
*comment TO ADD MORE: *rand TEAM_X 0 100
[b]Randomised[/b]
*gosub displayLeaderBoard 5
*gosub orderTeams 
[b]Ordered[/b]
*gosub displayLeaderBoard 5

[b]Top Two Teams[/b]
*gosub displayLeaderBoard 2
*ending 
*comment ::----------------------------::	
*label orderTeams
*comment currently accounts for five teams; call on this using *gosub orderTeams
*comment first create backups for so that ranks can be swapped correctly:
*temp rank_1_backup      rank_1
*temp rank_2_backup      rank_2
*temp rank_3_backup      rank_3
*temp rank_4_backup      rank_4
*temp rank_5_backup      rank_5
*comment TO ADD MORE: *temp rank_x_backup rank_x
*comment ::----------------------------::
*comment grab actual score values for each team
*temp value_1 "${{rank_1}}"
*temp value_2 "${{rank_2}}"
*temp value_3 "${{rank_3}}"
*temp value_4 "${{rank_4}}"
*temp value_5 "${{rank_5}}"
*comment TO ADD MORE: *temp value_x "${{rank_x}}
*comment ::----------------------------::
*gosub loopFullPass
*return			
*comment ::----------------------------::			
*label loopFullPass
*comment this runs through each ranking and checks values until they are correctly 
*comment ordered greatest to least; when orders_swapped is false, we return
*temp orders_swapped false
*if (value_2 > value_1)
	*set rank_1 "${rank_2_backup}" 
	*set rank_2 "${rank_1_backup}"
	*set value_2 "${{rank_2}}"
	*set value_1 "${{rank_1}}"
	*set rank_1_backup rank_1
	*set rank_2_backup rank_2
	*set orders_swapped true
*if (value_3 > value_2)	
	*set rank_2 "${rank_3_backup}" 
	*set rank_3 "${rank_2_backup}"
	*set value_3 "${{rank_3}}"
	*set value_2 "${{rank_2}}"
	*set rank_2_backup rank_2
	*set rank_3_backup rank_3
	*set orders_swapped true
*if (value_4 > value_3)	
	*set rank_3 "${rank_4_backup}" 
	*set rank_4 "${rank_3_backup}"
	*set value_4 "${{rank_4}}"
	*set value_3 "${{rank_3}}"
	*set rank_3_backup rank_3
	*set rank_4_backup rank_4
	*set orders_swapped true
*if (value_5 > value_4)	
	*set rank_4 "${rank_5_backup}" 
	*set rank_5 "${rank_4_backup}"
	*set value_5 "${{rank_5}}"
	*set value_4 "${{rank_4}}"
	*set rank_4_backup rank_4
	*set rank_5_backup rank_5
	*set orders_swapped true
*comment TO ADD MORE: grab the code from "*if (value_5 > value_4)" to the line 
*comment above this comment block and copy it so that it is the next *if. 
*comment increase the value_x and rank_x by 1 for everything in that section. 
*comment repeat if you want to add another team
*if (orders_swapped)
	*goto loopFullPass
*return
*comment ::----------------------------::	
*label displayLeaderBoard
*comment just a reusable leaderboard
[b]Leaderboard:[/b][n/]
*params
*comment param_1 should equal the number of teams you want to show up in the leaderboard
*comment	ex. 2 == top two teams 
*if (param_1 >= 1)
	1. $!{rank_1}: ${{rank_1}}[n/]
*if (param_1 >= 2)
	2. $!{rank_2}: ${{rank_2}}[n/]
*if (param_1 >= 3)
	3. $!{rank_3}: ${{rank_3}}[n/]
*if (param_1 >= 4)
	4. $!{rank_4}: ${{rank_4}}[n/]
*if (param_1 >= 5)
	5. $!{rank_5}: ${{rank_5}}[n/]
*comment TO ADD MORE: copy paste previous 2 lines and increase numbers by 1. 
*comment param_1 does not get increased
*return	

For it to work you need all the starting variables and everything under the labels “orderTeams” and “loopFullPass”, everything else is just the stuff I used to test/display.

1 Like

This thread might be useful: Seeking simple code to rank a series of stats - #8 by andymwhy

1 Like

Actually just thought of a way this could also bug out. Is there any chance of getting equal scores? If it doesn’t really matter you could just make sure you account for it with a more than or equal statement, but if you want to potentially have equal firsts etc in tthe leaderboard, you may need a whole extra routine to account for that. (If it’s not that important, I’d just choose who you want to win in the case of a draw and code it that way, easier.)

1 Like

Yes there is a chance to get equal scores

Thaank you guys for the help.But I had already done all those methods before posting this topic.
I figured 4 variable are easy to rank,get to 5 and more and the code get’s messy :laughing::laughing: you won’t even know you are the one who wrote it when you review it.