我有一个整数数组,我需要对数组进行排序,但只需要前12个数字。 这是我到目前为止的代码:
cantidad_bolillas_array = Bolillas_Array.length;
if(cantidad_bolillas_array > 12) {
array1 = Bolillas_Array.slice(0, 12);
array2 = Bolillas_Array.slice(12, cantidad_bolillas_array - 1);
array1.sort();
Bolillas_Array = array1.concat(array2);
}else{
Bolillas_Array.sort();
}
但这不适合我。实现这一目标的最佳方法是什么?
答案 0 :(得分:0)
只需进行一些更改,请参阅更新的代码示例中的注释:
var cantidad_bolillas_array:Number = Bolillas_Array.length;
if(cantidad_bolillas_array > 12) {
var array1:Array = Bolillas_Array.slice(0, 12);
// when you use slice, remember that the endIndex is exclusive, not inclusive
// that means you use "last index + 1" to get all the elements in the array
var array2:Array = Bolillas_Array.slice(12, cantidad_bolillas_array);
// make sure you sort numerically, otherwise you'll see stuff like
// "1, 11, 111, 2, 22, 222" etc
array1.sort(Array.NUMERIC);
Bolillas_Array = array1.concat(array2);
}else{
// once again, make sure to sort numerically
Bolillas_Array.sort(Array.NUMERIC);
}