public compareTo()

in packages/web-components/fast-router/src/recognizer.ts [231:312]


    public compareTo(b: Candidate<T>): -1 | 1 | 0 {
        const statesA = this.states;
        const statesB = b.states;

        for (
            let iA = 0, iB = 0, ii = Math.max(statesA.length, statesB.length);
            iA < ii;
            ++iA
        ) {
            let stateA = statesA[iA];
            if (stateA === void 0) {
                return 1;
            }

            let stateB = statesB[iB];
            if (stateB === void 0) {
                return -1;
            }

            let segmentA = stateA.segment;
            let segmentB = stateB.segment;
            if (segmentA === null) {
                if (segmentB === null) {
                    ++iB;
                    continue;
                }

                if ((stateA = statesA[++iA]) === void 0) {
                    return 1;
                }

                segmentA = stateA.segment!;
            } else if (segmentB === null) {
                if ((stateB = statesB[++iB]) === void 0) {
                    return -1;
                }

                segmentB = stateB.segment!;
            }

            if (segmentA.kind < segmentB.kind) {
                return 1;
            }

            if (segmentA.kind > segmentB.kind) {
                return -1;
            }

            ++iB;
        }

        const skippedStatesA = this.skippedStates;
        const skippedStatesB = b.skippedStates;

        const skippedStatesALen = skippedStatesA.length;
        const skippedStatesBLen = skippedStatesB.length;

        if (skippedStatesALen < skippedStatesBLen) {
            return 1;
        }

        if (skippedStatesALen > skippedStatesBLen) {
            return -1;
        }

        for (let i = 0; i < skippedStatesALen; ++i) {
            const skippedStateA = skippedStatesA[i];
            const skippedStateB = skippedStatesB[i];

            if (skippedStateA.length < skippedStateB.length) {
                return 1;
            }

            if (skippedStateA.length > skippedStateB.length) {
                return -1;
            }
        }

        // This should only be possible with a single pattern with multiple consecutive star segments.
        // TODO: probably want to warn or even throw here, but leave it be for now.
        return 0;
    }