sort(compareFunction) : compareFunction sample

var arr = [10, 5, 30, 25];
(function(){

    arr.sort(); //[10,25,30,5]
    var i = 'hoge1';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(){return -1;}); //[10,25,30,5]
    var i = 'hoge2';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(){return 0;}); //[10,25,30,5]
    var i = 'hoge3';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(){return 1;}); //[5,30,25,10]
    var i = 'hoge4';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return -1;}); //[5,30,25,10]
    var i = 'hoge5';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return 0;}); //[5,30,25,10]
    var i = 'hoge6';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return 1;}); //[10,25,30,5]
    var i = 'hoge7';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a-b;}); //[5,10,25,30]
    var i = 'hoge8';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return b-a;}); //[30,25,10,5]
    var i = 'hoge8';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a>b;}); //[5,10,25,30]
    var i = 'hoge10';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a<b;}); //[30,25,10,5]
    var i = 'hoge11';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

}).call(this);


(function(){
    var arr = [
        ['CC','300','yy'],
        ['AA','200','xx'],
        ['BB','100','zz'],
    ];
    arr.sort(function(a,b){return a[0]-b[0]}); //[CC,300,yy,AA,200,xx,BB,100,zz]
    var i = 'hoge12';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a[1]-b[1]}); //[BB,100,zz,AA,200,xx,CC,300,yy]
    var i = 'hoge13';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a[2]-b[2]}); //[BB,100,zz,AA,200,xx,CC,300,yy]
    var i = 'hoge14';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return b[0]-a[0]}); //[BB,100,zz,AA,200,xx,CC,300,yy]
    var i = 'hoge15';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return b[1]-a[1]}); //[CC,300,yy,AA,200,xx,BB,100,zz]
    var i = 'hoge16';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return b[2]-a[2]}); //[CC,300,yy,AA,200,xx,BB,100,zz]
    var i = 'hoge17';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a[0]<b[0]}); //[CC,300,yy,BB,100,zz,AA,200,xx]
    var i = 'hoge18';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a[1]<b[1]}); //[CC,300,yy,AA,200,xx,BB,100,zz]
    var i = 'hoge19';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a[2]<b[2]}); //[BB,100,zz,CC,300,yy,AA,200,xx]
    var i = 'hoge20';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a[0]>b[0]}); //[AA,200,xx,BB,100,zz,CC,300,yy]
    var i = 'hoge21';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a[1]>b[1]}); //[BB,100,zz,AA,200,xx,CC,300,yy]
    var i = 'hoge22';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

    arr.sort(function(a,b){return a[2]>b[2]}); //[AA,200,xx,CC,300,yy,BB,100,zz]
    var i = 'hoge23';
    document.getElementById(i).textContent=i + '= [' + arr + ']';

}).call(this);

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

TagTimes

Simple time tracking tool
Developed by Namu Works