documentation/api/weaver.js-1.0.0-pre/weaver.js
function( cmp ){
      var self = this;
      var P = this._private.pass[0].length;
      var subsize = this.spreadSize();
      var N = this.length;

      cmp = cmp || function( a, b ){ // default comparison function
        if( a < b ){
          return -1;
        } else if( a > b ){
          return 1;
        }

        return 0;
      };

      self.require( cmp, '_$_$_cmp' );

      return self.spread(function( split ){ // sort each split normally
        var sortedSplit = split.sort( _$_$_cmp );
        resolve( sortedSplit );

      }).then(function( joined ){
        // do all the merging in the main thread to minimise data transfer

        // TODO could do merging in separate threads but would incur add'l cost of data transfer
        // for each level of the merge

        var merge = function( i, j, max ){
          // don't overflow array
          j = Math.min( j, P );
          max = Math.min( max, P );

          // left and right sides of merge
          var l = i;
          var r = j;

          var sorted = [];

          for( var k = l; k < max; k++ ){

            var eleI = joined[i];
            var eleJ = joined[j];

            if( i < r && ( j >= max || cmp(eleI, eleJ) <= 0 ) ){
              sorted.push( eleI );
              i++;
            } else {
              sorted.push( eleJ );
              j++;
            }

          }

          // in the array proper, put the sorted values
          for( var k = 0; k < sorted.length; k++ ){ // kth sorted item
            var index = l + k;

            joined[ index ] = sorted[k];
          }
        };

        for( var splitL = subsize; splitL < P; splitL *= 2 ){ // merge until array is "split" as 1

          for( var i = 0; i < P; i += 2*splitL ){
            merge( i, i + splitL, i + 2*splitL );
          }

        }

        return joined;
      });
    }
function( cmp ){
      var self = this;
      var P = this._private.pass[0].length;
      var subsize = this.spreadSize();
      var N = this.length;

      cmp = cmp || function( a, b ){ // default comparison function
        if( a < b ){
          return -1;
        } else if( a > b ){
          return 1;
        }

        return 0;
      };

      self.require( cmp, '_$_$_cmp' );

      return self.spread(function( split ){ // sort each split normally
        var sortedSplit = split.sort( _$_$_cmp );
        resolve( sortedSplit );

      }).then(function( joined ){
        // do all the merging in the main thread to minimise data transfer

        // TODO could do merging in separate threads but would incur add'l cost of data transfer
        // for each level of the merge

        var merge = function( i, j, max ){
          // don't overflow array
          j = Math.min( j, P );
          max = Math.min( max, P );

          // left and right sides of merge
          var l = i;
          var r = j;

          var sorted = [];

          for( var k = l; k < max; k++ ){

            var eleI = joined[i];
            var eleJ = joined[j];

            if( i < r && ( j >= max || cmp(eleI, eleJ) <= 0 ) ){
              sorted.push( eleI );
              i++;
            } else {
              sorted.push( eleJ );
              j++;
            }

          }

          // in the array proper, put the sorted values
          for( var k = 0; k < sorted.length; k++ ){ // kth sorted item
            var index = l + k;

            joined[ index ] = sorted[k];
          }
        };

        for( var splitL = subsize; splitL < P; splitL *= 2 ){ // merge until array is "split" as 1

          for( var i = 0; i < P; i += 2*splitL ){
            merge( i, i + splitL, i + 2*splitL );
          }

        }

        return joined;
      });
    }
function ( cmp ) { var self = this ; var P = this . _private . pass [ 0 ] . length ; var subsize = this . spreadSize ( ) ; var N = this . length ; cmp = cmp || function ( a , b ) { // default comparison function if ( a < b ) { return - 1 ; } else if ( a > b ) { return 1 ; } return 0 ; } ; self . require ( cmp , '_$_$_cmp' ) ; return self . spread ( function ( split ) { // sort each split normally var sortedSplit = split . sort ( _$_$_cmp ) ; resolve ( sortedSplit ) ; } ) . then ( function ( joined ) { // do all the merging in the main thread to minimise data transfer // TODO could do merging in separate threads but would incur add'l cost of data transfer // for each level of the merge var merge = function ( i , j , max ) { // don't overflow array j = Math . min ( j , P ) ; max = Math . min ( max , P ) ; // left and right sides of merge var l = i ; var r = j ; var sorted = [ ] ; for ( var k = l ; k < max ; k ++ ) { var eleI = joined [ i ] ; var eleJ = joined [ j ] ; if ( i < r && ( j >= max || cmp ( eleI , eleJ ) <= 0 ) ) { sorted . push ( eleI ) ; i ++ ; } else { sorted . push ( eleJ ) ; j ++ ; } } // in the array proper, put the sorted values for ( var k = 0 ; k < sorted . length ; k ++ ) { // kth sorted item var index = l + k ; joined [ index ] = sorted [ k ] ; } } ; for ( var splitL = subsize ; splitL < P ; splitL *= 2 ) { // merge until array is "split" as 1 for ( var i = 0 ; i < P ; i += 2 * splitL ) { merge ( i , i + splitL , i + 2 * splitL ) ; } } return joined ; } ) ; }
sorts the passed array using a divide and conquer strategy
sorts the passed array using a divide and conquer strategy