Skip to content

Instantly share code, notes, and snippets.

@panicbus
Created October 20, 2016 18:40
Show Gist options
  • Save panicbus/7a36ace2650cd8471dabbf08c1030fe5 to your computer and use it in GitHub Desktop.
Save panicbus/7a36ace2650cd8471dabbf08c1030fe5 to your computer and use it in GitHub Desktop.

Revisions

  1. panicbus created this gist Oct 20, 2016.
    20 changes: 20 additions & 0 deletions sort.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    Array.prototype.nSort = nSort;
    // [].prototype.nSort works too

    function nSort(){
    return this.sort(
    function numSort(n1, n2){
    if (n1 < n2) return -1;
    if (n1 > n2) return 1;
    return 0;
    }
    );
    }

    var numbers = [];
    numbers[0] = 20;
    numbers[1] = 22;
    numbers[2] = 21;
    numbers[3] = 19;

    console.log(numbers.nSort())