Q:

jQuery script not working with pre populated Array

I hope I am not repeating an existing question, I have tried really hard to find what I need on the site, but now feel I need to ask the question, so here goes, I hope you guys can help me out :s
I have an array;
var 1Results = somecontent;
var 2Results = somecontent;
var 3Results = somecontent;
var 4Results = somecontent;

var nomResults = 1Results + 2Results + 3Results + 4Results;

I have a script that is supposed to weed out the duplicate numbers and display them (in sorted_arr);
    var arr = nomResults;
    var sorted_arr = arr.sort(); // You can define the comparing function here. 
                                 // JS by default uses a crappy string compare.
    var results = [];
    for (var i = 0; i < arr.length - 1; i++) {
        if (sorted_arr[i + 1] == sorted_arr[i]) {
            results.push(sorted_arr[i]);
        }
    }

This script doesn't work, however is I change the script to this;
    var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
    var sorted_arr = arr.sort(); // You can define the comparing function here. 
                                 // JS by default uses a crappy string compare.
    var results = [];
    for (var i = 0; i < arr.length - 1; i++) {
        if (sorted_arr[i + 1] == sorted_arr[i]) {
            results.push(sorted_arr[i]);
        }
    }

It works fine, any ideas why .sort() won't work with my pre popluated array?
Any help would be greatly appreciated.

A:

You have to use brackets notation to add elements to your new Array like :
var arr = [Results1, Results2...];

Or Array.prototype.push() :
var arr = [];
arr.push(Results1);
arr.push(Results2);
//...

Plus you can use a specific function for sorting, either declaring a new function compare :
function compare(a, b) {
  return a - b;
}

Which will use actual values of your array (and not Strings, as specified in your comments).
Then you pass it to sort :
arr = arr.sort(compare);

Or directly use anonymous function, if you don't need it more than one time :
arr = arr.sort(function(a, b) {
  return a - b;
});

