我正在尝试实现排序管道,因为我已经遵循了这个文档 http://www.advancesharp.com/blog/1211/angular-2-search-and-sort-with-ngfor-repeater-with-example
并根据我创建了一个管道并在组件中导入了该管道并在html中附加了管道
它按日期排序,但不是按照正确的顺序排序。[
public class Game extends JFrame {
Game(){
super("MineSweeper 0.0");
setLocation(300, 300);
setResizable(false);
setLayout(new GridLayout(mainclass.rows, mainclass.cols));
Dimension d = new Dimension(30, 30);
for(int i = 0; i < mainclass.rows; i++){
for(int j = 0; j < mainclass.cols; j++){
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setPreferredSize(d);
add(panel);
JButton boton = new JButton();
boton.addActionListener(new ListenerCasillas(panel));
panel.add(boton);
}
}
setVisible(true);
pack();
}
}
// component
import { OrderByPipe } from '../../pipes/order-by.pipe';
import { Component, OnInit } from '@angular/core';
export class SurveyInfoComponent implements OnInit {
surveyRecords: Array<any>;
isDesc: boolean = false;
column: string = "date";
direction: number;
constructor() {}
ngOnInit() {
this.surveyRecords = [
{ surveyID: 1, surveyName: "survey1", date: "1-10-2016" },
{ surveyID: 9, surveyName: "survey9", date: "1-12-2016" },
{ surveyID: 3, surveyName: "survey3", date: "9-10-2016" },
{ surveyID: 5, surveyName: "survey5", date: "11-10-2016" },
{ surveyID: 6, surveyName: "survey6", date: "16-10-2016" },
{ surveyID: 7, surveyName: "survey7", date: "19-10-2016" },
{ surveyID: 8, surveyName: "survey8", date: "21-10-2016" },
{ surveyID: 4, surveyName: "survey4", date: "10-10-2016" },
{ surveyID: 10, surveyName: "survey10", date: "1-10-2017" },
{ surveyID: 2, surveyName: "survey2", date: "5-10-2016" },
{ surveyID: 11, surveyName: "survey11", date: "5-10-2017" },
{ surveyID: 12, surveyName: "survey12", date: "15-10-2017" },
{ surveyID: 13, surveyName: "survey13", date: "25-10-2017" }
];
}
sort(property) {
console.log(property);
this.isDesc = !this.isDesc; //change the direction
this.column = property;
this.direction = this.isDesc ? 1 : -1;
}
}
//pipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'orderBy'
})
export class OrderByPipe implements PipeTransform {
transform(surveyRecords: Array<any>, args?: any): any {
// return null;
console.log(surveyRecords);
return surveyRecords.sort(function(a, b) {
a['date']= Date.parse(a['date']);
b['date']= Date.parse(b['date']);
if (a[args.property] < b[args.property]) {
return -1 * args.direction;
} else if (a[args.property] > b[args.property]) {
return 1 * args.direction;
} else {
return 0;
}
});
}
}