function sorter(a, b) {
    if (typeof a === 'undefined') {
      return A_COMES_FIRST;
    }
    if (typeof b === 'undefined') {
      return B_COMES_FIRST;
    }

    const aUsesRefs = a.original && usesReferences(a.original[valueKey]);
    const bUsesRefs = b.original && usesReferences(b.original[valueKey]);

    // -----BOTH REFERENCE-----
    if (aUsesRefs && bUsesRefs) {
      // Collect references for 'a' and 'b'
      const aRefs = getReferences(a.original[valueKey], tokens, {
        unfilteredTokens,
        warnImmediately: false,
      });
      const bRefs = getReferences(b.original[valueKey], tokens, {
        unfilteredTokens,
        warnImmediately: false,
      });

      // 'a' references 'b' -> 'b' should come first
      if (aRefs.some((aRef) => aRef.name === b.name)) {
        return B_COMES_FIRST;
      }
      // 'b' references 'a' -> 'a' should come first
      if (bRefs.some((bRef) => bRef.name === a.name)) {
        return A_COMES_FIRST;
      }

      // 'a' and 'b' reference, but not each other. Recurse to next level
      return sorter(aRefs[0], bRefs[0]);
    }

    // ----- ONLY ONE REFERENCES -----
    // 'a' references, 'b' doesn't -> 'b' should come first
    if (aUsesRefs) {
      return B_COMES_FIRST;
    }

    // 'b' references, 'a' doesn't -> 'a' should come first
    if (bUsesRefs) {
      return A_COMES_FIRST;
    }

    // no references, keep existing order
    return 0;
  }
The sorter function is recursive to account for multiple levels of nesting
@param {Token} a
@param {Token} b
@returns {number}