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 自定义管道不';I don’我不能在角上正常工作_Sorting_Angular_Angular2 Pipe - Fatal编程技术网

Sorting 自定义管道不';I don’我不能在角上正常工作

Sorting 自定义管道不';I don’我不能在角上正常工作,sorting,angular,angular2-pipe,Sorting,Angular,Angular2 Pipe,管道必须按照name属性对对象数组进行排序 按.pipe.ts排序: import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'sortBy' }) export class SortByPipe implements PipeTransform { private name: string; transform(array: Array<any>, args: string[]): Arra

管道必须按照
name
属性对对象数组进行排序

按.pipe.ts排序:

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'sortBy'
})
export class SortByPipe implements PipeTransform {
  private name: string;

  transform(array: Array<any>, args: string[]): Array<any> {
    array.sort((a: any, b: any) => {
      if (a.name < b.name) {
        return -1;
      } else if (a.name > b.name) {
        return 1;
      } else {
        return 0;
      }
    });
    return array;
  }
}
 <table>
    <tr *ngFor="let elem of _values | sortBy">
      <td>{{ elem.name }}</td>
      <td>{{ elem.ts }}</td>
      <td>{{ elem.value }}</td>
    </tr>
  </table>
//other imports
import { SortByPipe } from './sort-by.pipe';

@NgModule({
  declarations: [
    SortByPipe
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: []
})
export class AppModule { }
[{
  "name": "t10",
  "ts": 1476778297100,
  "value": "32.339264",
  "xid": "DP_049908"
}, {
  "name": "t17",
  "ts": 1476778341100,
  "value": "true",
  "xid": "DP_693259"
}, {
  "name": "t16",
  "ts": 1476778341100,
  "value": "true",
  "xid": "DP_891890"
}];
对象数组:

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'sortBy'
})
export class SortByPipe implements PipeTransform {
  private name: string;

  transform(array: Array<any>, args: string[]): Array<any> {
    array.sort((a: any, b: any) => {
      if (a.name < b.name) {
        return -1;
      } else if (a.name > b.name) {
        return 1;
      } else {
        return 0;
      }
    });
    return array;
  }
}
 <table>
    <tr *ngFor="let elem of _values | sortBy">
      <td>{{ elem.name }}</td>
      <td>{{ elem.ts }}</td>
      <td>{{ elem.value }}</td>
    </tr>
  </table>
//other imports
import { SortByPipe } from './sort-by.pipe';

@NgModule({
  declarations: [
    SortByPipe
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: []
})
export class AppModule { }
[{
  "name": "t10",
  "ts": 1476778297100,
  "value": "32.339264",
  "xid": "DP_049908"
}, {
  "name": "t17",
  "ts": 1476778341100,
  "value": "true",
  "xid": "DP_693259"
}, {
  "name": "t16",
  "ts": 1476778341100,
  "value": "true",
  "xid": "DP_891890"
}];
它不会正确地对对象进行排序,但也不会抛出任何错误
也许我的文件有问题


感谢您的帮助

问题在于,您正在管道化元素,而不是阵列本身

你应该改变这个

<tr *ngFor="let elem of _values | sortBy">

为此:

<tr *ngFor="let elem of (_values | sortBy)">

您是否尝试添加
纯:假

这使得Angular不在乎输入是否改变

@Pipe({
  name: "sort",
  pure: false
})

也许你想做的是一个整数比较。。阅读此文,您是否希望顺序为
't10',t16',t17'
?@ps2goat是的,我的朋友,我认为问题在于您“ngFor”,因为您在元素中。。不是array@H.Doe您得到的结果是什么?它只是没有对数组进行排序。但我认为问题出在其他地方。我实现的排序功能基本上不起作用。它根本不会对这些属性进行排序。我已经用您发布的数据测试了排序功能,对我来说效果很好。也许你可以放置一些
console.log
,看看会发生什么,甚至更好,调试它。当我
console.log
变量仍然未排序时。这是有效的,以前我遇到了一个问题,即_值没有传递给我的管道过滤器函数。就是这样!听起来好像其他人的管道过滤器功能有问题。