【问题标题】:Angular2 ng2 smart table sorting as numericAngular2 ng2智能表排序为数字
【发布时间】:2025-12-09 17:05:03
【问题描述】:

我已经实现ng2 smart table grid 上市

https://akveo.github.io/ng2-smart-table/#/documentation

在这里我想整合 2 列的排序。为此,我使用了代码

 storeid: {
  title: 'Store #',
  sort: true
},

但这不是数字排序。它排序为1,10,11... 而不是1,2,3... 对于特定列,comparefunction 是否有任何示例。

请帮帮我。

【问题讨论】:

    标签: angular ng2-smart-table


    【解决方案1】:

    我正在使用以下函数对包含文本和数字的列进行排序:

    compareFunction(direction: any, a: any, b: any) {
    
      let first = isNaN(Number(a)) ? a.toLowerCase() : Number(a);
      let second = isNaN(Number(b)) ? b.toLowerCase() : Number(b);
    
      if (first < second || (!isNaN(Number(a)) && isNaN(Number(b)))) {
        return -1 * direction;
      }
      if (first > second || (isNaN(Number(a)) && !isNaN(Number(b)))) {
        return direction;
      }
    
    
      return 0;
    } 
    

    它首先显示数字(数字排序),然后显示文本(1、2、10、a、b、c)

    【讨论】: