Array.prototype.sort()

//array.sort(compareFunction);
(function(){
    var arr = [2, 10, 4, 20, 34];
    console.log(arr.sort());
    console.log(arr.sort(function(x,y){
        return x-y;
    }));
}).call(this);

(function(){
    var arr = ['a', 'd', 'c', 'e', 'b'];
    console.log(arr.sort());
    console.log(arr.sort(function(x,y){
        return x-y;
    }));
}).call(this);

(function(){
    var arr = ['apple', 'device', 'cool', 'ear', 'buzz'];
    console.log(arr.sort());
    console.log(arr.sort(function(x,y){
        return x-y;
    }));
}).call(this);

(function(){
    var arr = ['apples', 'bananas', 'Cherries'];
    console.log(arr.sort());
    console.log(arr.sort(function(x,y){
        return x-y;
    }));
}).call(this);

See the Pen js: sort() by nwstcode (@nwst) on CodePen.

TagTimes

Simple time tracking tool
Developed by Namu Works