字母数字排序打字稿

问题描述 投票:0回答:1

输入 -> const s = ['2.1.1.a', '2.1.a', '2.1.1.d', 'A2.2', 'A2.1', 2.1.1.c', ' 2.1.b', '2.1.1.b']

排序后的预期输出 -> const s = ['2.1.a', '2.1.b', '2.1.1.a', '2.1.1.b', '2.1.1.c', '2.1 .1.d'、'A2.1'、'A2.2']

使用以下方法排序后输出 -> const s = ['2.1.b', '2.1.a', '2.1.1.d', '2.1.1.c', '2.1.1.b' , '2.1.1.a', , 'A2.1', 'A2.2']

下面是我尝试排序以获取输出的角度方法,有人可以告诉我这段代码有什么问题吗

private REGEXP_SPECIAL_CHAR = /^[A-Za-z]\d$/;

bubbleSort(collection: any[], property: string = 'code'): any[] {
        let memo = null;

        if (!collection.length) { return collection; }

        for (let outer = 0; outer < collection.length; outer++) {
            for (let inner = 0; inner < collection.length; inner++) {
                if (this.compare(collection[outer][property], collection[inner][property]) === -1) {
                    memo = collection[outer];
                    collection[outer] = collection[inner];
                    collection[inner] = memo;
                }
            }
        }

        return collection;
    }
    private compare(v: any, w: any): number {
        const vCollection = v.split('.');
        const wCollection = w.split('.');
        const depth = Math.min(vCollection.length, wCollection.length);
        let res = 0;

        for (let i = 0; i < depth; i++) {
            const vElement = vCollection[i];
            const wElement = wCollection[i];
            const shouldCompareNumbers = !isNaN(Number(vElement)) && !isNaN(Number(wElement));
            const shouldCompareChars = this.isSpecialSymbol(vElement, wElement) || (!isNaN(Number(vElement)) && !isNaN(Number(wElement)));

            if (shouldCompareNumbers) {
                if (Number(vElement) < Number(wElement)) {
                    res = -1;
                    break;
                } else if (Number(vElement) > Number(wElement)) {
                    res = 1;
                    break;
                }
            } else if (shouldCompareChars) {
                if (vElement < wElement) {
                    res = -1;
                    break;
                } else if (vElement > wElement) {
                    res = 1;
                    break;
                }
            } else {
                if (vElement < wElement) {
                    res = 1;
                    break;
                } else if (vElement > wElement) {
                    res = -1;
                    break;
                }
            }
        }

        if (res === 0) {
            // check depth
            if (vCollection.length > wCollection.length) {
                res = 1;
            } else if (vCollection.length < wCollection.length) {
                res = -1;
            } else {
                res = 0;
            }
        }
        return res;
    }

    private isSpecialSymbol(v: string, w: string): boolean {
        return this.REGEXP_SPECIAL_CHAR.test(v) || this.REGEXP_SPECIAL_CHAR.test(w);
    }

javascript angular typescript
1个回答
0
投票

可以将[a-z]替换为0[a-z],再次排序替换

sort(s:string[])
{
   return s.map(x=>x.replace(/([a-z])/g,'0$1'))
           .sort()
           .map(x=>x.replace(/0([a-z])/g,'$1'))
}
© www.soinside.com 2019 - 2024. All rights reserved.