javascript - 角度排序列
问题描述
现在这就是我正在使用的东西,因为我无法让它工作。无论如何,我认为这并不坏,但仍然:
在我的 app.component.html 我有:
<table border="1">
<!-- ADD HEADERS -->
<tr>
<th><a href="#" (click)="send()">Name</a>
</th>
<th>Goverment form</th>
</tr>
</table>
在我的 app.component.ts 我有:
}
return 0;
});
}
--------
Data:
-------
export interface Country {
Name:string;
GovernmentForm:string;
}
export class AppComponent {
public Countries: Country[];
解决方案
您需要更改排序函数来比较.Name
属性,而不是整个对象。你可以这样做:
this.Countries = this.Countries.sort((n1, n2) => {
if (n1.Name > n2.Name) {
return 1;
}
if (n1.Name < n2.Name) {
return -1;
}
return 0;
});
推荐阅读
- apache-spark - 如何在python中使用spark找出每个月的总金额
- raku - 如何重新加载 REPL 中使用的模块?
- react-native - 反应原生地图,通过点击标记在滚动视图卡上重新定位
- eclipse - Runner eclipse插件不显示未分类的文件夹
- java - 遍历列表并将 Callables 提交给 ExecutorService
- c# - 为什么在尝试使用实体框架执行查询时指定的强制转换无效?
- ruby-on-rails - SSL_connect 返回=1 errno=0 状态=错误:证书验证失败
- javascript - PhantomJS 中的滚动页面
- git - 如何从旧提交中恢复文件?
- r - 存储和解释 lm() 模型的结果