x


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!

more ▼

asked Mar 14 '11 at 03:00 PM

raul corrales gravatar image

raul corrales
268 25 28 34

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Hi Raul,

Use arrays and use sort() function. Here it is one example link text

Good to know some Spanish people are around ;)

more ▼

answered Mar 16 '11 at 10:29 PM

Uzquiano gravatar image

Uzquiano
480 22 25 35

gracias Uzquiano, aqui estamos los espaoles escondidos xdd

Mar 17 '11 at 02:15 AM raul corrales
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 14 '11 at 03:28 PM

Jesse Anders gravatar image

Jesse Anders
7.3k 7 17 48

thanks for reply!

Mar 17 '11 at 02:23 AM raul corrales
(comments are locked)
10|3000 characters needed characters left

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;
}
more ▼

answered Mar 17 '11 at 12:05 AM

Statement gravatar image

Statement ♦♦
20.2k 35 71 176

thanks for the example!

Mar 17 '11 at 02:18 AM raul corrales

This is not an example of a good way of doing this so I feel it's quite ironic you accepted this answer :P

Mar 17 '11 at 12:04 PM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3571
x356
x61

asked: Mar 14 '11 at 03:00 PM

Seen: 531 times

Last Updated: Mar 16 '11 at 10:29 PM