Sort variables from highest to lowest
Hi,
Does anyone have a Javascript for taking a series of number variables, corresponding text variables of their labels, sorting them, then populating, say "varTop3_1_Label", "varTop3_1_Score", "varTop3_2_Label", "varTop3_2_Score", "varTop3_3_Label", and "varTop3_3_Score"?
I have been asked to create a "Top 3" from values selected on 18 sliders, displaying the top 3 Slider's "name"s and the Slider value.
I have tackled this before on a timed quiz using triggers, where each quiz question has a countdown timer and the user gets fewer points the longer it takes to answer if they answer correctly. Then their score is placed on a leaderboard. This was a mammoth task, and something I'm struggling to recreate with this sliders activity.
I'm guessing this can be accomplished more efficiently using Javascript. It doesn't need to be dynamic - what I'm aiming for is a "click this button to view your top 3" button.
I have little-to-no knowledge of Javascript, but this kind of request comes in from time-to-time, so I would like to have a working script that can be adapted whenever these self-evaluation type requests come in.
I'm guessing it would need to comprise of
- player.GetVars for variables of the slider names and their values,
- a JS var that creates a string from the values
- a .sort() function to reorder them
- the reordered string to be written to JS vars
- Those JS values be player.Setvar'd to the SL variables "varTop3_1_Label", "varTop3_1_Score", "varTop3_2_Label" etc.
I just don't know how to construct it, especially ensuring that the values are linked to their label.
I have tried adapting some found JS from elsewhere on ELH, but I think it was designed to operate differently to what I'm trying to accomplish:
var player = GetPlayer();
var Slider1 = player.GetVar("Slider1");
var Slider2 = player.GetVar("Slider2");
var Slider3 = player.GetVar("Slider3");
var Slider4 = player.GetVar("Slider4");
var Slider5 = player.GetVar("Slider5");
var Slider6 = player.GetVar("Slider6");
var Slider7 = player.GetVar("Slider7");
var Slider8 = player.GetVar("Slider8");
var Slider9 = player.GetVar("Slider9");
var Slider10 = player.GetVar("Slider10");
var Slider11 = player.GetVar("Slider11");
var Slider12 = player.GetVar("Slider12");
var Slider13 = player.GetVar("Slider13");
var Slider14 = player.GetVar("Slider14");
var Slider15 = player.GetVar("Slider15");
var Slider16 = player.GetVar("Slider16");
var Slider17 = player.GetVar("Slider17");
var Slider18 = player.GetVar("Slider18");
var Points = [Slider1, Slider2, Slider3, Slider4, Slider5. Slider6, Slider7, Slider8, Slider9, Slider10, Slider11, Slider12, Slider13, Slider14, Slider15, Slider16, Slider17, Slider18];
Points.sort(function(a , b){return b-a});
for ( i=O;i <Points.length;i++)
document. write(Points[i] + "<br >" ) ;
player.Setvar ("Points",Points) ;