Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 对日期数组排序_Sorting_Date - Fatal编程技术网

Sorting 对日期数组排序

Sorting 对日期数组排序,sorting,date,Sorting,Date,如何对日期数组进行排序。您使用的语法非常正确,并且应该如何排序 工作中,你可以准确地演示你在做什么 问题很可能与用于构造Date对象的releaseDate的值有关。排序函数中console.log的输出是什么?我更新了@jeanpaula创建的plunker,以便您可以看到差异。在他的plunker中,他正在更新数组本身,而不是数组中的对象,因此我认为它需要更新 如果查看,可以看到差异,但它确实起作用,对数组中的对象进行排序。我想你的数据一定有问题。我添加了DatePipe来格式化日期,这样您

如何对日期数组进行排序。

您使用的语法非常正确,并且应该如何排序

工作中,你可以准确地演示你在做什么


问题很可能与用于构造
Date
对象的
releaseDate
的值有关。排序函数中console.log的输出是什么?

我更新了@jeanpaula创建的plunker,以便您可以看到差异。在他的plunker中,他正在更新数组本身,而不是数组中的对象,因此我认为它需要更新

如果查看,可以看到差异,但它确实起作用,对数组中的对象进行排序。我想你的数据一定有问题。我添加了DatePipe来格式化日期,这样您就可以使用它在UI中轻松查看您的日期是否正确

Plunkr代码:

//our root app component
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { DatePipe } from '@angular/common';

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <button (click)="sort()">Sort</button>
      <ul>
         <li *ngFor="let date of dates">{{date | date: 'dd MM yyyy'}}</li>
      </ul>
      <ul>
         <li *ngFor="let customer of customers">
         {{customer.name}} -
         {{customer.releaseDate | date: 'dd MM yyyy'}}</li>
      </ul>      
    </div>
  `,
})
export class App {
  name:string;

  customers: Array<any> = [
    {"name": 'Mickey', "releaseDate": new Date('2012-07-12')},
    {"name": 'Minnie', "releaseDate": new Date('2013-07-12')},
    {"name": 'Donald', "releaseDate": new Date('2010-07-12')},
    {"name": 'Pluto', "releaseDate": new Date('2014-07-12')}
    ]

  dates:Array<Date> = [
    new Date('2012-07-12'),
    new Date('2013-07-12'),
    new Date('2010-07-12'),
    new Date('2014-07-12')
  ];
  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }

  sort() {
    this.dates.sort((a:Date, b:Date) => new Date(a).getTime() - new Date (b).getTime());
    this.customers.sort((a: Customer, b: Customer) =>
      new Date(a.releaseDate).getTime() - new Date (b.releaseDate).getTime());
    console.log(this.customers);
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}
//我们的根应用程序组件
从“@angular/core”导入{Component,NgModule,VERSION}
从“@angular/platform browser”导入{BrowserModule}
从“@angular/common”导入{DatePipe};
@组成部分({
选择器:“我的应用程序”,
模板:`
你好{{name}
分类
    {{date | date:'dd-MM-yyyy'}
    {{customer.name}- {{customer.releaseDate}日期:'dd-MM-yyyy'}
`, }) 导出类应用程序{ 名称:字符串; 客户:阵列=[ {“姓名”:“米奇”,“发布日期”:新日期('2012-07-12'), {“姓名”:“米妮”,“发布日期”:新日期('2013-07-12'), {“姓名”:“唐纳德”,“发布日期”:新日期('2010-07-12'), {“名称”:“冥王星”,“发布日期”:新日期('2014-07-12')} ] 日期:数组=[ 新日期('2012-07-12'), 新日期('2013-07-12'), 新日期('2010-07-12'), 新日期('2014-07-12') ]; 构造函数(){ this.name=`Angular!v${VERSION.full}` } 排序(){ this.dates.sort((a:Date,b:Date)=>newdate(a.getTime()-newdate(b.getTime()); this.customers.sort((a:Customer,b:Customer)=> 新日期(a.releaseDate.getTime()-新日期(b.releaseDate.getTime()); console.log(this.customers); } } @NGD模块({ 导入:[BrowserModule], 声明:[App], 引导:[应用程序] }) 导出类AppModule{}
输出是一个日期数组,格式如下:1520290800000这很奇怪。。。如果数据是正确的,它应该可以工作。。。你能用你想排序的数据样本更新你的问题吗?@JeanPaulA。谢谢,但是没有你的代码我是做不到的!:)我注意到你接受了我的回答(谢谢)。但是如果你解决了这个问题,你可以在你的问题中添加一个编辑部分来解释问题是什么(因为JeanPaulA和我都认为你的代码是正确的,但是你的数据不正确)。谢谢