我有一个候选对象,它具有属性
candidateid:number;
name:string;
我希望能够根据name
属性对这些对象的数组进行排序。在Angular 2中,我该如何实现这一点?
我有一个候选对象,它具有属性
candidateid:number;
name:string;
我希望能够根据name
属性对这些对象的数组进行排序。在Angular 2中,我该如何实现这一点?
它与普通的JavaScript相同。您仍然可以使用箭头函数使其更加简洁。
x.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0)
或者使用 localeCompare 方法。
x.sort((a, b) => a.name.localeCompare(b.name))
localeCompare
。 - user663031localeCompare
- 一定是英国人写的... 在 Node 中不起作用。 - JonathanProperty 'foo' does not exist on type 'object'.ts(2339)
的错误。 - Timonodejs
中不起作用吗?Node和浏览器JS使用相同的JS引擎.. - Timo