# Array.sortOn with string and integer flawed

**URL:** <https://forum.kirupa.com/t/array-sorton-with-string-and-integer-flawed/188978>\
**Category:** Uncategorized\
**Created:** [May 12, 2006, 9:11am UTC](https://forum.kirupa.com/t/array-sorton-with-string-and-integer-flawed/188978 "2006-05-12T09:11:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasper](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@Rasper](https://forum.kirupa.com/u/Rasper)\
**Post date:** [May 12, 2006, 9:11am UTC](https://forum.kirupa.com/t/array-sorton-with-string-and-integer-flawed/188978/1 "2006-05-12T09:11:55Z")

</div>

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:
