I have an array like;
["IL0 Foo", "PI0 Bar", "IL10 Baz", "IL3 Bob says hello"]
And need to sort it so it appears like;
["IL0 Foo", "IL3 Bob says hello", "IL10 Baz", "PI0 Bar"]
I have tried a sort function;
function compare(a,b) {
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
but this gives the order
["IL0 Foo", "IL10 Baz", "IL3 Bob says hello", "PI0 Bar"]
I have tried to think of a regex that will work but can't get my head around it.
If it helps the format will always be 2 letters, x amount of numbers, then any number of characters.
Pad numbers in string with leading zeros, then sort normally.
Add one more alternative (why not):
jsfiddle example