Simple top 3 scores from an array to textfield

52 Views Asked by At

How to make top 3 scores from an array to the 3 different text fields and sort them highest to lowest scores? My code:

var top3Array:Array = [];

var totalScore = int((numeratorCounter/denominatorCounter)*100)/100;

fucntion scoreBoard(): void{
    top3Array.sortOn("totalScore");
    for(i:int = 0, i < 3; i++)
    {
        count1.text = String(count);
        score1.text = String(top3Array[0]);
    }
}
1

There are 1 best solutions below

0
Will On

Its a little unclear from your question but it sounds like you want to add all of your scores to an array, take the top 3 and display them in some text fields?

As @Organis mentioned, the contents of your array does determine what kind of sort you would need to perform.

If the scores array just contains a list of numbers then you need only use the .sort() function rather than .sortOn()

On the assumption that each score is pushed to an array named allScores each time the player dies you could use code similar to this:

// All of the scores
const allScores:Array = [24, 10, 16, 28, 5, 29];

// Sort scores into descending numeric order.
const sortedScores:Array = allScores.sort(Array.DESCENDING | Array.NUMERIC);
// >> 29,28,24,16,10,5

// Get the first 3 elements of the array - i.e. the 3 highest numbers.
const top3scores:Array = sortedScores.slice(0, 3);
// >> 29,28,24

// Set the text fields' contents to the top scores.
score1.text = top3scores[0];
score2.text = top3scores[1];
score3.text = top3scores[2];