VadimDez / ngx-order-pipe

▼ Angular 5+ orderBy pipe
https://vadimdez.github.io/ngx-order-pipe/
MIT License
243 stars 57 forks source link

Feature request to switch it to case-insensitive #26

Closed oztek22 closed 6 years ago

ghc20 commented 7 years ago

I modified ngx-order.pipe.ts to handle the case sensitivity issue for sorting string:

private sortArray(value: any[], expression?: any, reverse?: boolean): any[] {
    const isDeepLink = expression && expression.indexOf('.') !== -1;

    if (isDeepLink) {
        expression = OrderPipe.parseExpression(expression);
    }

    let array: any[] = value.sort((a: any, b: any): number => {
        if (!expression) {
            return a > b ? 1 : -1;
        }

        if (!isDeepLink) {
            let aVal = a[expression], bVal = b[expression];
            if (typeof aVal === 'string') {
                aVal = aVal.toLowerCase();
            }

            if (typeof bVal === 'string') {
                bVal = bVal.toLowerCase();
            }

            return aVal > bVal ? 1 : -1;
        }

        return OrderPipe.getValue(a, expression) > OrderPipe.getValue(b, expression) ? 1 : -1;
    });

    if (reverse) {
        return array.reverse();
    }

    return array;
}