如何基于打字稿中的键对对象数组进行排序 [英] How to sort object array based on key in typescript
本文介绍了如何基于打字稿中的键对对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有属性的候选对象
I have a candidate object with properties
candidateid:number;
name:string;
我希望根据名称对这些对象进行排序
财产。如何在角度2中的TypeScript中实现此目的?
I wish to sort an array of such objects based on the name
property. How can I achieve this in TypeScript in angular 2?
推荐答案
它与普通的旧javascript相同。您仍然可以使用箭头功能使其更简洁。
It's the same as plain old javascript. You can still use an arrow function to make it more concise.
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))
这篇关于如何基于打字稿中的键对对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文