简体   繁体   English

如何在文本框中显示数组的前3个最大值?

[英]How to display the top 3 biggest values of an array in a textbox?

So I'm creating a Flash game and I save the scores in sharedObject. 因此,我正在创建一个Flash游戏,并将分数保存在sharedObject中。 I have successfully saved the scores to an Array. 我已成功将分数保存到数组。 Here is the code: 这是代码:

import flash.net.SharedObject;
var shared:SharedObject = SharedObject.getLocal("sharedmean");
var tempArray:Array = new Array();

if (shared.data.score) {
 tempArray = shared.data.score;
}

tempArray.push(scoreTotal);
shared.data.score = tempArray;

trace(tempArray);
shared.flush();
shared.close();

and the trace result is: 跟踪结果为:

500,100,0,100,100,0,300

I've created the highscore textbox and want to display the 3 biggest values from that array. 我已经创建了高分文本框,并希望显示该数组中的3个最大值。 I want the textbox to display this: 我希望文本框显示以下内容:

Highscore 1: 500
Highscore 2: 300
Highscore 3: 100

How do I do it? 我该怎么做? Thank you. 谢谢。

The sort method, sorts values in an array according to its parameters. sort方法根据其参数对数组中的值进行sort Here we use Array.NUMERIC parameter to sort by numeric values.also we push Array.DESCENDING parameter to sort value from biggest to smallest.That makes an easier access to the three first values: 在这里,我们使用Array.NUMERIC参数按数字值进行排序,同时我们将Array.DESCENDING参数按值从最大到最小的Array.DESCENDING进行排序,这样可以更轻松地访问前三个值:

tempArray.sort(Array.NUMERIC | Array.DESCENDING);
//don't forget to push " | " between parameters

now 现在

textField.text= "Highscore 1:"+tempArray[0]+"\n Highscore 2:"+tempArray[1]+"\n Highscore 3:"+tempArray[2];

IH☺PE this helps ! IH☺PE这有帮助!

快速的Google搜索将为您带来以下内容

tempArray.sort(Array.NUMERIC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM