在打字稿中对一组对象进行排序?

新手上路,请多包涵

如何在 TypeScript 中对对象数组进行排序?

具体来说,根据一个特定属性对数组对象进行排序,在这种情况下是 nome (“name”) 还是 cognome (“surname”)?

 /* Object Class*/
export class Test{
     nome:String;
     cognome:String;
}

/* Generic Component.ts*/
tests:Test[];
test1:Test;
test2:Test;

this.test1.nome='Andrea';
this.test2.nome='Marizo';
this.test1.cognome='Rossi';
this.test2.cognome='Verdi';

this.tests.push(this.test2);
this.tests.push(this.test1);

谢谢!

原文由 Maurizio Rizzo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 414
1 个回答
var arr = [110,5,72,14,12]

升序排序

arr.sort(( a, b ) => a > b ? 1 : -1 )

降序排序

arr.sort(( a, b ) => a < b ? 1 : -1 )

原文由 Syed Faran Raza 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进