0

I have an array in which the elements have like ID value "imm1", "imm2", "imm3"..."imm10"

the problem is that the sortOn method consider "imm10" lower than "imm2" because it consider 1 and 0 separatly. Then I tryed to write

immarray.sortOn("id", Array.NUMERIC)

but it don't apply the order correctly and I don't know why... maybe because my ID propriety have an alphanumeric value? Then, how I can sort i t correctly?

I want this result:

imm1
imm2
imm3
...
imm10
imm11
imm12
etc... 
1
  • are there any other prefixes other than "imm"? Commented Jan 14, 2012 at 17:36

1 Answer 1

2

You'll have to create a custom sorting function. In that function you'll have to select the numeric values in the Strings and sort on them. In short:

immarray.sort(sortNumeric);

private function sortNumeric(a:String, b:String):int {
    return a.match(/\d+/g)[0] - b.match(/\d+/g)[0];
}

Some explanation: the regular expression \d+ finds the numeric characters in the String. Note that this example is not programmed defensively and assumes that every String is formatted in the same way (e.g. 'imm10').

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.