|
Don’t forget that the Array.sort()
method sorts using string values by default, so even when sorting an array of
numbers or ints, they will be sorted as if they were strings. To get numeric
sorting, so that “10” comes after “2”, use the
Array.NUMERIC sort option, e.g.: myArray.sort(Array.NUMERIC); Here’s the relevant example from the
ActionScript documentation for Array.sort(): The following code creates the Array object numbers
with elements [3,5,100,34,10]. A call to sort()
without any parameters sorts alphabetically, producing the undesired result [10,100,3,34,5].
To properly sort numeric values, you must pass the constant NUMERIC
to the sort() method, which sorts numbers
as follows: [3,5,10,34,100]. Note:
The default behavior of the sort()
function is to handle each entity as a string. The Array.NUMERIC
argument does not actually convert other data types to the Number data type; it
simply allows the sort algorithm to recognize numbers. var numbers:Array = new Array(3,5,100,34,10); trace(numbers); // 3,5,100,34,10 numbers.sort(); trace(numbers); // 10,100,3,34,5 numbers.sort(Array.NUMERIC); trace(numbers); // 3,5,10,34,100 Francis From: Thanks Tom, I believe array indices are ints, not strings. However, I also
believe that datagrid indices are strings, because when I used the Array.sort()
function on the DataGrid.selectedIndices array, the order was all messed up.
For example, “10” came before “2”. For that reason, I
ended up writing a custom sort function to get around the issue. Thanks again! Jim From: Try
either of these two options: On 6/8/06, Jim Robson <[EMAIL PROTECTED]> wrote: I don't understand why DataGrid.selectedIndices
returns an array of Strings. I thought that indices were generally integers.
(For example, Array.indexOf returns an int.) Can anyone explain why the selected index of a
DataGrid is a String? When "dgTest" is a DataGrid instance, the
following code generates a compiler error: var arr:Array = dgTest.selectedIndices; for(var i:int in arr){ // do something } The error that the compiler returns is as follows: "Implicit coercion of a value of type String to
an unrelated type int. " -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
SPONSORED LINKS
YAHOO! GROUPS LINKS
|
- Re: [flexcoders] selectedIndices are Strings??? Tom Bray
- RE: [flexcoders] selectedIndices are Strings??? Jim Robson
- RE: [flexcoders] selectedIndices are Strings??? Francis Cheng

