I’m using the sortOn method to sort my array.
concider this array:
[FONT=Courier New][COLOR=dimgray]var list:Array = new Array();[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “1”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “4”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “12”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “23”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “9”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.sortOn(“id”);[/COLOR][/FONT]
This will return the following:
1, 12, 23, 4, 9
because the sorting happens on the first digit. so 12 is first, than 4
this can be fixed by using the following sort:
[FONT=Courier New][COLOR=dimgray]var list:Array = new Array();[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “1”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “4”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “12”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “23”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “9”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.sortOn(“id”, [COLOR=blue]Array.NUMERIC[/COLOR]);[/COLOR][/FONT]
This will return the following:
1, 4, 9, 12, 23
which is fine
But when you decide to use an id that consists of a string + integer, this way of working doesn’t work anymore . . .
[FONT=Courier New][COLOR=dimgray]var list:Array = new Array();[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar1”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar4”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar12”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar23”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar9”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=#696969]list.sortOn(“id”[/COLOR][COLOR=#696969]);[/COLOR][/FONT]
[FONT=Courier New][COLOR=#696969]and [/COLOR][/FONT]
[FONT=Courier New][COLOR=#696969][FONT=Courier New][COLOR=dimgray]var list:Array = new Array();[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar1”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar4”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar12”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar23”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=dimgray]list.push( {id: “fubar9”} );[/COLOR][/FONT]
[FONT=Courier New][COLOR=#696969]list.sortOn(“id”, [/COLOR][COLOR=blue]Array.NUMERIC[/COLOR][COLOR=#696969]);[/COLOR][/FONT]
[/COLOR][/FONT]
Will both return:
fubar1, fubar12, fubar23, fubar4, fubar9
How can this be fixed? I want my sort to have the following result:
fubar1, fubar4, fubar9, fubar12, fubar23
I think this is a pretty big shortcoming of the sortOn method :hangover: