index.js
function order_stack (stack) {
    // Find a handle with a before or after property
    for (var i = 0; i < stack.length; i++) {
      var handle = stack[i].handle;
      if (handle._first) {
        // remove handle from current position
        var mid = stack.splice(i, 1)[0];
        // insert it at begining of stack
        stack.unshift(mid);

        // remove property so we don't order it again later
        delete handle._first;
        // for debugging
        handle._moved_first = true;

        // Continue ordering for remaining handles
        return order_stack (stack);
      }
      else if (handle._before || handle._after) {
        var position = null;
        if (handle._before) {
          position = '_before';
        }
        else if (handle._after) {
          position = '_after';
        }

        var label = handle[position];

        for (var j = 0; j < stack.length; j++) {
          if (stack[j].handle.label === label) {
            // insert before index = j
            // insert after index = j + 1
            var new_index = j;
            if (position == '_after') new_index++;

            // move handle in new position
            // http://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another
            stack.splice(new_index, 0, stack.splice(i, 1)[0]);

            // remove _before/_after property so we don't order ad infinitum
            handle['_moved' + position] = handle[position]; // for debugging
            break;
          }
        }
        delete handle[position];
        // Continue ordering for remaining handles
        return order_stack (stack);
      }
    }
    // didn't find any handle with a before/after property => done ordering
    return true;
  }
function order_stack (stack) {
    // Find a handle with a before or after property
    for (var i = 0; i < stack.length; i++) {
      var handle = stack[i].handle;
      if (handle._first) {
        // remove handle from current position
        var mid = stack.splice(i, 1)[0];
        // insert it at begining of stack
        stack.unshift(mid);

        // remove property so we don't order it again later
        delete handle._first;
        // for debugging
        handle._moved_first = true;

        // Continue ordering for remaining handles
        return order_stack (stack);
      }
      else if (handle._before || handle._after) {
        var position = null;
        if (handle._before) {
          position = '_before';
        }
        else if (handle._after) {
          position = '_after';
        }

        var label = handle[position];

        for (var j = 0; j < stack.length; j++) {
          if (stack[j].handle.label === label) {
            // insert before index = j
            // insert after index = j + 1
            var new_index = j;
            if (position == '_after') new_index++;

            // move handle in new position
            // http://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another
            stack.splice(new_index, 0, stack.splice(i, 1)[0]);

            // remove _before/_after property so we don't order ad infinitum
            handle['_moved' + position] = handle[position]; // for debugging
            break;
          }
        }
        delete handle[position];
        // Continue ordering for remaining handles
        return order_stack (stack);
      }
    }
    // didn't find any handle with a before/after property => done ordering
    return true;
  }
lookup connect.stack if there is a fn with before or after properties and move it at right place @todo: optimize