Javascript sort() with concat()

Why doesn't this sort in numeric order after concat?

var hege = [34, 12];
var stale = [1, 78, 8, 4];
var children = hege.concat(stale).sort(); //1,12,34,4,78,8

the .sort method sorts elements alphabetically


.sort(function(a,b){return a - b})

var children = hege.concat(stale).sort(function(a,b){return a - b}); //1,4,8,12,34,78

check this fiddle

The sort() method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort() method sorts the values as strings in alphabetical and ascending order. This works well for strings ("Apple" comes before "Banana").

Try this

hege.concat(stale).sort(function(a,b){return (+a)-(+b)})

Here we are converting each value in number & sorting as numbers.

Because by default javascript sort does ordering as string, you have to specify how you want to sort.

The concat method creates a new array consisting of the elements in the object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array). It does not recurse into nested array arguments.

function myFunction() {
    var hege = [34, 12];
    var stale = [1, 78, 8, 4];
    var children = hege.concat(stale).sort((hege, stale) => hege>stale); 
    document.getElementById("demo").innerHTML= children;

Separators make Array#join() a very flexible way to concatenate strings. If you want to join together a variable number of strings, you should generally use join() rather than a for loop with +. String#concat() JavaScript strings have a built-in concat() method. The concat() function

Definition and Usage. The concat() method is used to join two or more strings. This method does not change the existing strings, but returns a new string containing the text of the joined strings.

str.concat(str2 [, strN]) Parameters str2 [, strN] Strings to concatenate to str. Return value. A new string containing the combined text of the strings provided. Description. The concat() function concatenates the string arguments to the calling string and returns a new string. Changes to the original string or the returned string don't

columns = ['letter', 'number', 'animal']) >>> df3 letter number animal 0 c 3 cat 1 d 4 dog >>> pd. concat ([df1, df3], sort = False) letter number animal 0 a 1 NaN 1 b 2 NaN 0 c 3 cat 1 d 4 dog Combine DataFrame objects with overlapping columns and return only those that are shared by passing inner to the join keyword argument.