【问题标题】:Angular 5 Sort by date, text, numbersAngular 5 按日期、文本、数字排序
【发布时间】:2018-08-22 13:49:49
【问题描述】:

我已经实现了可重复使用的排序功能,按数字和文本排序工作正常,但按日期排序失败。

orderBy(array: Array<any>, fieldName: string, direction: string) {
  return array.sort((a, b) => {
    let objectA: number|string = '';
    let objectB: number|string = '';
    [objectA, objectB] = [a[fieldName], b[fieldName]];
    let valueA = isNaN(+objectA) ? objectA.toString().toUpperCase() : +objectA;
    let valueB = isNaN(+objectB) ? objectB.toString().toUpperCase() : +objectB;
    return (valueA < valueB ? -1 : 1) * (direction == 'asc' ? 1 : -1);
  });
}

如何按日期、文本数字和特殊字符排序。

【问题讨论】:

  • 我建议使用 moment.js

标签: javascript arrays angular sorting


【解决方案1】:

试试这个:

orderBy(array: Array<any>, fieldName: string, direction: string) {
  return array.sort((a, b) => {
    let objectA: number|string|Date = '';
    let objectB: number|string|Date = '';
    [objectA, objectB] = [a[fieldName], b[fieldName]];

    // I assume that objectA and objectB are of the same type
    return typeof objectA === 'string' ? objectA.localeCompare(objectB) : objectA - objectB;
  });
}

如果无法识别Date 类型,您可能需要在您的compilerOptions 中添加es6 条目,详情请参阅this answer

更新

如果您要排序的所有值都是字符串,请尝试以下操作:

orderBy(array: Array<any>, fieldName: string, direction: string) {
  return array.sort((a, b) => {
    let objectA: number|string|Date = '';
    let objectB: number|string|Date = '';
    // map function here will convert '15/12/2018' into '2018/12/15'
    // so we can compare values as strings, numbers and strings
    // will remain unchanged
    [objectA, objectB] = [a[fieldName], b[fieldName]].map(i => i.split('/').reverse().join('/'));

    return isNaN(+objectA) ? objectA.localeCompare(objectB) : +objectA - +objectB;
  });
}

【讨论】:

  • "objectA.localeCompare(objectB) : objectA - objectB;"给出一个错误.. 不工作
  • 如果 objectA 和 objectB 是字符串,这应该可以。在浏览器的调试器控制台中尝试'aaa'.localeCompare('ddd')。在您的情况下,也许 objectA 或 objectB 不是字符串?但由于 typescript Date 类型,它应该会产生错误。请使用您的数组和代码创建 Plunker 或 stackblitz,我会尽力提供帮助。另见developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 哪个错误为您提供“objectA.localeCompare(objectB) : objectA - objectB;”?
【解决方案2】:

我认为这可能是最好的,因为 localCompare 函数 对于之后返回正数,对于之前返回负数,对于等于返回 0(在本例中我正在比较 .name ,它是 this.array 中的对象 ab 的属性)

this.array.sort((a, b) => { 
 return a.name.localeCompare(b.name);
});

【讨论】:

    【解决方案3】:

    如果你使用的类型脚本比你可以这样做,还没有尝试过,但你可以试用

    public orderby(fieldName : string)
    {
        switch (typeof obj[fieldName].constructor.name) {           
                case "String":
                    array.sort();
                case "Number":
                      array.sort(function(a,b){
                           return a-b);
                     });
                case "Date"://you can check type and add accordingly 
                //as suggested by @Andriy  its going to be object 
                     array.sort(function(a,b){
                           return new Date(b.date) - new Date(a.date);
                     });
                default:
                    throw new Error("Type of T is not a valid return type!");
            }
        } else {
            throw new Error("Key '" + key + "' does not exist!");
        }
    }
    

    对于日期排序,我喜欢这样做,转换日期中的值并获取返回零、正值或负值的减法运算的值

    array.sort(function(a,b){
      return new Date(b.date) - new Date(a.date);
    });
    

    【讨论】:

    • 这将只对日期列进行排序。我也需要文字和数字。动态列和值。
    • @Gopalakrishnan - 所以你需要通用方法??
    • @Gopalakrishnan - 所以你使用的是打字稿??
    • 是的。将此函数作为服务调用
    • @Gopalakrishnan - 您可以试用解决方案可能对您有用
    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多