index.js
function grouped(array, groups, order) {
  if (!groups) { groups = [returnFalse]; }
  var sorted = [];
  var ordered = [];
  var remaining = array.slice();
  var unmatchedPosition = groups.indexOf('unmatched');
  groups.forEach(function(criteria, index) {
    if (index === unmatchedPosition) { return; }
    var tieBreakers = [];
    if (index !== groups.length - 1) {
      tieBreakers = groups.slice(index + 1);
      if (index < unmatchedPosition) {
        tieBreakers.splice(unmatchedPosition - index - 1, 1);
      }
    }
    var spliced = splice(remaining, criteria, tieBreakers);
    var matched = spliced.matched;
    var unmatched = spliced.unmatched;
    sorted[index] = matched;
    remaining = unmatched;
  });
  if (unmatchedPosition === -1) { unmatchedPosition = sorted.length; }
  sorted[unmatchedPosition] = remaining;
  if (Array.isArray(order)) {
    order.forEach(function(position, index) {
      ordered[index] = sorted[position];
    });
  } else {
    ordered = sorted;
  }
  return ordered.reduce(function(flat, group) {
    return flat.concat(group);
  }, []);
}
function grouped(array, groups, order) {
  if (!groups) { groups = [returnFalse]; }
  var sorted = [];
  var ordered = [];
  var remaining = array.slice();
  var unmatchedPosition = groups.indexOf('unmatched');
  groups.forEach(function(criteria, index) {
    if (index === unmatchedPosition) { return; }
    var tieBreakers = [];
    if (index !== groups.length - 1) {
      tieBreakers = groups.slice(index + 1);
      if (index < unmatchedPosition) {
        tieBreakers.splice(unmatchedPosition - index - 1, 1);
      }
    }
    var spliced = splice(remaining, criteria, tieBreakers);
    var matched = spliced.matched;
    var unmatched = spliced.unmatched;
    sorted[index] = matched;
    remaining = unmatched;
  });
  if (unmatchedPosition === -1) { unmatchedPosition = sorted.length; }
  sorted[unmatchedPosition] = remaining;
  if (Array.isArray(order)) {
    order.forEach(function(position, index) {
      ordered[index] = sorted[position];
    });
  } else {
    ordered = sorted;
  }
  return ordered.reduce(function(flat, group) {
    return flat.concat(group);
  }, []);
}
function grouped ( array , groups , order ) { if ( ! groups ) { groups = [ returnFalse ] ; } var sorted = [ ] ; var ordered = [ ] ; var remaining = array . slice ( ) ; var unmatchedPosition = groups . indexOf ( 'unmatched' ) ; groups . forEach ( function ( criteria , index ) { if ( index === unmatchedPosition ) { return ; } var tieBreakers = [ ] ; if ( index !== groups . length - 1 ) { tieBreakers = groups . slice ( index + 1 ) ; if ( index < unmatchedPosition ) { tieBreakers . splice ( unmatchedPosition - index - 1 , 1 ) ; } } var spliced = splice ( remaining , criteria , tieBreakers ) ; var matched = spliced . matched ; var unmatched = spliced . unmatched ; sorted [ index ] = matched ; remaining = unmatched ; } ) ; if ( unmatchedPosition === - 1 ) { unmatchedPosition = sorted . length ; } sorted [ unmatchedPosition ] = remaining ; if ( Array . isArray ( order ) ) { order . forEach ( function ( position , index ) { ordered [ index ] = sorted [ position ] ; } ) ; } else { ordered = sorted ; } return ordered . reduce ( function ( flat , group ) { return flat . concat ( group ) ; } , [ ] ) ; }
Does a full sort based on an array of criteria, plus the option to set the position of any unmatched items. Can be used with an anymatch-compatible criteria array, or an array of those arrays.
Does a full sort based on an array of criteria plus the option to set the position of any unmatched items . Can be used with an anymatch - compatible criteria array or an array of those arrays .