简体   繁体   English

Angular 6 按日期对对象数组进行排序

[英]Angular 6 Sort Array of object by Date

I try to sort the array object by date for my Angular 6 application.我尝试按日期对我的 Angular 6 应用程序的数组对象进行排序。 The data has string format.数据具有字符串格式。 I wonder if there is an existing module to perform sort in Angular or we have to build sort function it in Typescript.我想知道是否有一个现有的模块可以在 Angular 中执行排序,或者我们必须在 Typescript 中构建排序功能。

Angular Template角度模板

<app-item *ngFor="let item of someArray"></app-item>

The Array数组

[
  {
    CREATE_TS: "2018-08-15 17:17:30.0",
    Key1: "Val1",
    Key2: "Val2",
  },
  {
    CREATE_TS: "2018-08-15 17:25:30.0",
    Key1: "Val1",
    Key2: "Val2",
  },
  {
    CREATE_TS: "2018-08-15 17:28:30.0",
    Key1: "Val1",
    Key2: "Val2",
  }
]

You can use Array.sort for sort data.您可以使用Array.sort对数据进行排序。

I have created a demo on Stackblitz .我在Stackblitz上创建了一个演示。 I hope this will help/guide to you/others.我希望这会对您/其他人有所帮助/指导。

component.ts组件.ts

  data = [
    {
      CREATE_TS: "2018-08-15 17:17:30.0",
      Key1: "Val1",
      Key2: "Val2",
    },
    {
      CREATE_TS: "2018-08-15 17:25:30.0",
      Key1: "Val1",
      Key2: "Val2",
    },
    {
      CREATE_TS: "2018-08-15 17:28:30.0",
      Key1: "Val1",
      Key2: "Val2",
    }
  ]

  get sortData() {
    return this.data.sort((a, b) => {
      return <any>new Date(b.CREATE_TS) - <any>new Date(a.CREATE_TS);
    });
  }

component.html组件.html

<div *ngFor="let item of sortData">
  {{item.Key1}} -- {{item.CREATE_TS}} 
</div>

you can use the sort function for arrays, it takes in compare function.您可以对数组使用sort函数,它接受比较函数。 Parse the Date string into a date object and sort by it.将日期字符串解析为日期对象并按它排序。

read more about here阅读更多关于这里

var myArr = [


{
    CREATE_TS: "2018-08-15 17:17:30.0",
    Key1: "Val1",
    Key2: "Val2",
  },
  {
    CREATE_TS: "2018-08-15 17:25:30.0",
    Key1: "Val1",
    Key2: "Val2",
  },
  {
    CREATE_TS: "2018-08-15 17:28:30.0",
    Key1: "Val1",
    Key2: "Val2",
  }
]
myArr.sort((val)=> {return new Date(val.CREATE_TS)})

Ascending上升

myArr.sort((val1, val2)=> {return new Date(val1.CREATE_TS) - new 
Date(val2.CREATE_TS)})

Descending降序

myArr.sort((val1, val2)=> {return new Date(val2.CREATE_TS) - new 
Date(val1.CREATE_TS)})

in addition to cryptic's answer, you will likely want to wrap the sorted values in an accessor for including in the template, adding a getter in your typescript class:除了神秘的答案之外,您可能还想将排序后的值包装在访问器中以包含在模板中,在您的打字稿类中添加一个 getter:

public get sortedArray(): YourItemType[] {
    return this.myArr.sort(...);
}

and in the template:并在模板中:

<app-item *ngFor="let item of sortedArray"></app-item>

alternately, you can sort the array as you get it into your component class and store the sorted version there, however the accessor pattern can be quite useful for dynamic sorting.或者,您可以在将数组放入组件类时对其进行排序并将排序后的版本存储在那里,但是访问器模式对于动态排序非常有用。

For recent first:对于最近的第一:

this.data.sort((a, b) => new Date(b.date1).getTime() - new Date(a.date1).getTime());

For OlderFirst:对于 OlderFirst:

this.data.sort((b, a) => new Date(b.date1).getTime() - new Date(a.date1).getTime());

Using this method in Typescript, you can easily sort date values in whatever order you'd like.在 Typescript 中使用此方法,您可以轻松地按您喜欢的任何顺序对日期值进行排序。 You can also sort any type of other data types, like number or string, by simply removing the new Date() and getTime methods.您还可以通过简单地删除新的 Date() 和 getTime 方法来对任何类型的其他数据类型(如数字或字符串)进行排序。

this.data.sort((a, b) => new Date(b.CREATE_TS).getTime() - new Date(a.CREATE_TS).getTime());

From looking through the docs, there doesn't seem to be any built-in array sorting.通过查看文档,似乎没有任何内置的数组排序。 However, you can do this in your template:但是,您可以在模板中执行此操作:

<app-item *ngFor="let item of someArray.sort(sortFunc)"></app-item>

And then in your component.ts file, define the function, because you cannot define functions in your template:然后在你的 component.ts 文件中,定义函数,因为你不能在你的模板中定义函数:

sortFunc (a, b) {
  return a.CREATE_TS - b.CREATE_TS
}

Edit: Simon K pointed out that the string format allows for straight comparison without coercing to Date and then to number.编辑:Simon K 指出字符串格式允许直接比较,而无需强制到日期然后再到数字。 My original equation (for scenarios where your date string isn't that convenient):我的原始公式(对于您的日期字符串不那么方便的情况):

return new Date(a.CREATE_TS).getTime() - new Date(b.CREATE_TS).getTime()

使用 moment.js,您可以使用

const newArr = myArr.sort((a, b) => moment(b.date).unix() - moment(a.date).unix());

您应该首先将日期解析为毫秒,然后执行排序,以避免“算术运算的左侧必须是‘any’、‘number’、‘bigint’或枚举类型”类型的打字稿错误当您返回 new Date(a.CREATE_TS) - new Date(b.CREATE_TS) 时:

someArray.sort((a: any, b: any) => { return Date.parse(a.CREATE_TS) - Date.parse(b.CREATE_TS) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM