Reorder numbers in string
I have a string with numbers:
10 9 8 11
I need to reorder them so they are in order like this:
8 9 10 11
So I'm using this function:
- strToAppend = strToAppend.split(' ');
- strToAppend.sort(function (a, b) {
- if (isNaN(a) || isNaN(b)) {
- return a > b ? 1 : -1;
- }
- return a - b;
- });
That works but the problem is that the string comes out like this:
891011
How can I reorder and still keep the spaces between the numbers?
8 9 10 11
I thought that "strToAppend" would be an array. If that would be the case I could loop through the numbers and add space, but when I'm trying to access one number using strToAppend[0] I get nothing.