typescript 如何按角度降序对对象数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49107215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to sort array of objects in descending order in angular?
提问by Asif Ahmed
public addition :any = [];
public display : any = [];
public object1 =['3','2','1'];
public value2 =['5','7','9','8'];
constructor(){}
ngOnInit(){
this.addition=this.object1.concat(this.value2);
this.display=this.addition.sort();
}
how to sort the array in descending order using typescript in angular 2 or above versions Complete code Here
如何在angular 2或更高版本中使用typescript按降序对数组进行排序 完整代码在这里
回答by Suren Srapyan
Just pass a function to the sort, based on which you want to sort the array
只需将一个函数传递给排序,根据您要对数组进行排序
.sort((a,b) => b - a)
回答by void
Your code
你的代码
this.display=this.addition.sort();
will sort the array in ascending order, to sort in descending order you need to pass a comparator function.
将按升序对数组进行排序,要按降序排序,您需要传递一个比较器函数。
this.display=this.addition.sort((a,b) => b - a);