在adf数据表中对数字列进行排序

时间:2019-05-23 06:48:34

标签: angular sorting datatable

我有以下方法,该方法仅对字符串列而不对数字列进行排序。

setSorting(sorting: DataSorting): void {
        this._sorting = sorting;

        if (sorting && sorting.key) {
            this._rows.sort((a: DataRow, b: DataRow) => {
                let left = a.getValue(sorting.key);
                if (left) {
                    left = (left instanceof Date) ? left.valueOf().toString() : left.toString();
                } else {
                    left = '';
                }

                let right = b.getValue(sorting.key);
                if (right) {
                    right = (right instanceof Date) ? right.valueOf().toString() : right.toString();
                } else {
                    right = '';
                }

                return sorting.direction === 'asc'
                    ? left.localeCompare(right)
                    : right.localeCompare(left);
            });
        }
    }

但是我也想对数字列进行排序。如何实现呢? 有人可以为此提供解决方案吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试两种解决方案:

   if (left) {
    if(left instanceof Number){
        left=left.toString();
    }else{
       left = (left instanceof Date) ? left.valueOf().toString() : left.toString();
                    } else {
                        left = '';
                    }
}

和rigth相同:

 if (right) {
 if(right instanceof Number){
        right=right.toString();
    }else{
                    right = (right instanceof Date) ? right.valueOf().toString() : right.toString();
                } else {
                    right = '';
                }
}

我认为必须工作