4

I'm trying to set up my page so a user is able to type in a string like "25a89ss15s9 8 63" and it alerts the user "25, 89, 15, 9, 8, 63" and then further alerts the user "8, 9, 15, 25, 63, 89". So I'm trying to separate the integers from the string specified, and then sort them. Any ideas on how I would separate them into an Array or something close? I've tried a few examples from similar questions, but they seem to only work with one integer.

Any help would be appreciated, thanks.

Josh Hayden
  • 43
  • 1
  • 3

5 Answers5

10
var string = "25a89ss15s9 8 63"; // Input
var list = string.match(/\d+/g); // Get a list of all integers
list.sort(function(x,y){         // Sort list
    return x - y;
});
// At this point, you have a sorted list of all integers in a string.

This code is using a RegEx (\d+ means: all consecutive digits = integers, /g means: select all occurrences). The String.match() method returns an array of all matched phrases, integers in this case.

Finally, the Array.sort method is invoked, passing a function as an argument, which sorts the array.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Note: `list` will be an array of numeric strings, not actual integers - it is not clear what the OP wants, but your post mentions `an array of matches phrases, integers in this case` which is slightly confusing as to what the actual result is. – jfriend00 Dec 07 '11 at 19:26
  • Thank you and everyone else so much! I appreciate it! – Josh Hayden Dec 07 '11 at 19:30
  • If you're looking for a comma separated string use 'join' from `Array.prototype`. `list.join(', ');` – Trevor Dec 08 '11 at 04:01
2

You can use a regular expression to parse out all numbers:

/\d+/g

(one or more numbers, and g means "all occurences")

Then you can sort them, but make sure you're using a custom function to avoid lexicographical sorting (which is the default):

var numbers = "25a89ss15s9 8 63".match(/\d+/g).sort(function(a, b) {return a - b});

You can alert like this:

alert("The numbers are: " + numbers.join(", "));
pimvdb
  • 151,816
  • 78
  • 307
  • 352
2

Try this:

var s = "25a89ss15s9 8 63";
var xs = s.match(/\d+/g); // => ["25", "89", "15", "9", "8", "63"]
xs.sort(function(x,y) { return x - y; });
xs.join(", "); // => "8, 9, 15, 25, 63, 89"
maerics
  • 151,642
  • 46
  • 269
  • 291
2

To end up with an actual array of numbers, I'd do this:

var str = "25a89ss15s9 8 63", 
    arr = str.match(/\d+/g)
             .map(Number)
             .sort(function(a,b){ return a-b; });

You'll need a compatibility patch for .map() available from MDN.

Note that this assumes no decimals points.

RightSaidFred
  • 11,209
  • 35
  • 35
1

Here is the linear search version:

var isNumber = false;

var number = "";
var string = "25a89ss15s9 8 63";

var numbers = [];

for (var i = 0; i <= string.length; ++i) {
    var c = string.charAt(i);
    if (c > '/' && c < ':') {
        number += c;
        isNumber = true;
    } else {
        isNumber = false;
    }

    if (!isNumber) {
        if (number != "") {
            numbers.push(number);
        } 
        number = "";
    }
}

// Non sorted array
alert(numbers);

// Sorted Array
alert(numbers.sort(function(x, y) {
    return x - y;
}));
Eder
  • 1,874
  • 17
  • 34