- Home /
How i can a variable clasification in javascript?
Hi to all. I have 10 variables:
var player1_score=34;
var player2_score=144;
var player3_score=4;
var player4_score=28;
var player5_score=33;
var player6_score=12;
var player7_score=36;
var player8_score=2;
var player9_score=321;
var player10_score=333;
How i can order the variables in max order? in javascript please
EXAMPLE:
| HIGHSCORES |
1 --PLAYER 10 ------------> 333
2 --PLAYER 9 ------------> 321
3 --PLAYER 2 ------------> 144
4 --PLAYER 7 ------------> 36
5 --......... ------------> .........
6 --......... ------------> .........
7 --......... ------------> .........
8 --......... ------------> .........
9 --......... ------------> .........
10 --......... ------------> .........
-
-
-
Thank you in advance!
Answer by Jesse Anders · Mar 14, 2011 at 03:28 PM
First, you'll want to use a container (such as an array or List) rather than separate variables.
Once you have the data stored in a container, you can use one of the .NET 'sort' functions to sort the elements of the container using a custom comparator that implements the desired sort order.
Answer by Statement · Mar 17, 2011 at 12:05 AM
Well, for the baptism of it, I'll extend your existing code to support sorting. While the code works (yes I even tested it), this is absolutely not good practice and should be considered mostly as a lesson in extremely redundant coding. The preferred way is of course using lists as the other answerers suggest (and I cheat a bit in my example, don't I?)
import System.Collections.Generic;
// The fire! It blinds my eyes! Must.. escape.. sunlight
// Players score var player1_score = 34; var player2_score = 144; var player3_score = 4; var player4_score = 28; var player5_score = 33; var player6_score = 12; var player7_score = 36; var player8_score = 2; var player9_score = 321; var player10_score = 333;
// Player numbers sorted by highest score. var score1_player : int; var score2_player : int; var score3_player : int; var score4_player : int; var score5_player : int; var score6_player : int; var score7_player : int; var score8_player : int; var score9_player : int; var score10_player : int;
function Start() { Sort(); }
function Sort() { var playerScore = new List.<KeyValuePair.<int, int> >();
playerScore.Add(new KeyValuePair.<int, int>(1, player1_score));
playerScore.Add(new KeyValuePair.<int, int>(2, player2_score));
playerScore.Add(new KeyValuePair.<int, int>(3, player3_score));
playerScore.Add(new KeyValuePair.<int, int>(4, player4_score));
playerScore.Add(new KeyValuePair.<int, int>(5, player5_score));
playerScore.Add(new KeyValuePair.<int, int>(6, player6_score));
playerScore.Add(new KeyValuePair.<int, int>(7, player7_score));
playerScore.Add(new KeyValuePair.<int, int>(8, player8_score));
playerScore.Add(new KeyValuePair.<int, int>(9, player9_score));
playerScore.Add(new KeyValuePair.<int, int>(10, player10_score));
playerScore.Sort(function(x : KeyValuePair.<int, int>, y : KeyValuePair.<int, int>)
{
return y.Value.CompareTo(x.Value);
});
score1_player = playerScore[0].Key;
score2_player = playerScore[1].Key;
score3_player = playerScore[2].Key;
score4_player = playerScore[3].Key;
score5_player = playerScore[4].Key;
score6_player = playerScore[5].Key;
score7_player = playerScore[6].Key;
score8_player = playerScore[7].Key;
score9_player = playerScore[8].Key;
score10_player = playerScore[9].Key;
}
This is not an example of a good way of doing this so I feel it's quite ironic you accepted this answer :P