首页 > 解决方案 > Angular中的排序排列

问题描述

我从数据库中获取一个数组(按降序排列),并将它们绘制在表格和图表的前面,如图所示:

在此处输入图像描述

但是在图表的一部分中,我想按升序排序,然后执行以下操作:

export class PruebasComponent implements OnInit {

  data: any[];
  dataSource: PeriodicElement[] = [];
  lineChartData: ChartDataSets[];
    .......
 ngOnInit() {
    this.loading = true;


    this.datos.getDesktopLimit().subscribe(
      res => {
        this.loading = false;
        this.data = [res];
        this.dataSource = this.data[0]; //aqui almaceno el arreglo y lo paso al componente html
        this.barChartData = true;
        this.getFilter(this.dataSource); // ejecuto la funcion getFilter()
      }
    )
  }

sort我按升序对其进行排序以将其传递给图表

  getFilter(data) {
    console.log(data);

     data.sort((a, b) => a.id - b.id); //ordenar de forma ascendente para la grafica

    for (let entry of data) {
      this.date.push(moment(entry.created).format('DD-MM-YYYY HH:mm'))
      this.time.push(entry.total_load_time * 0.001)
    }

    this.lineChartData =  [{ data: this.time, label: 'Time Render' }];
    this.lineChartLabels = this.date;
    this.loading = false
  }

它有效地命令我,但表格也按升序排序

在此处输入图像描述

标签: angulartypescript

解决方案


以下行什么都不做;

data.sort((a, b) => a.id - b.id);

您必须从中分配值:

sortedDataForGraph : any[];


...


sortedDataForGraph = data.sort((a, b) => a.id - b.id);

推荐阅读