I would like to sort an array of strings (in javascript) such that groups of digits within the strings are compared as integers not strings. I am not worried about signed or floating point numbers.

for example, the result should be ["a1b3","a9b2","a10b2","a10b11"] not ["a1b3","a10b11","a10b2","a9b2"]

The easiest way to do this seems to be splitting each string on boundaries around groups of digits. Is there a pattern I can pass to String.split to split on character boundaries without removing any characters?

"abc11def22ghi".split(/?/) = ["abc","11","def","22","ghi"];

Or is there another way to compare strings that does not involve splitting them up, perhaps by padding all groups of digits with leading zeros so they are the same length?

"aa1bb" => "aa00000001bb", "aa10bb" => "aa00000010bb"

I am working with arbitrary strings, not strings that have a specific arrangement of digit groups.

Edit:

I like the /(\d+)/ one liner from Gaby to split the array. How backwards compatible is that?

The solutions that parse the strings once in a way that can be used to rebuild the originals are much more efficient that this compare function. None of the answers handle some strings starting with digits and others not, but that would be easy enough to remedy and was not explicit in the original question.

Are the non-numeric parts always the same? If not, should the sorting algorithm sort them in ASCII order?
–
Mark ByersNov 12 '11 at 20:12

1

In your example, are extracting 13, 92, 102, 1011? Or is it more like 1.3, 9.2, 10.2, 10.11? I mean is the first number more significant or are the letters just ignored?
–
Lee KowalkowskiNov 12 '11 at 20:46

...oh you still want to sort on the non-integers too, I get it now...
–
Lee KowalkowskiNov 12 '11 at 22:01

I think OP still wants to sort on the non-digits too though.
–
Lee KowalkowskiNov 12 '11 at 22:04

@LeeKowalkowski - it's a fairly unclear question and the OP has not clarified. If my answer isn't what they're looking for, I've asked the OP to respond and clarify, but they have not.
–
jfriend00Nov 12 '11 at 22:07

Sorting occurs from left to right unless you create a custom algorithm. Letters or digits are compared digits first then letters.

However, what you want to accomplish as per your own example (a1, a9, a10) WON'T EVER HAPPEN. That would require you knowing the data before hand and spliting the string in every possible way before applying the sorting.

One final alternative would be:

a) break each and every string from left to right whenever is a change from letter to digit and vice versa; &
b) then start the sorting on those groups from RIGHT-TO-LEFT. That will be a very demanding algorithm. Can be done!

Finally, if you are the GENERATOR of the original "text", you should consider NORMALIZING the output where a1 a9 a10 could be outputed as a01 a09 a10. This way you could have full cotnrol of the final version of the algorithm.