admin管理员组文章数量:1289558
var arr = [
{ 'name': 'ashley', 'score': 5 },
{ 'name': 'jason', 'score': 9 },
{ 'name': 'ben', 'score': 1 },
{ 'name': 'jordan', 'score': 4 },
]
How can I reorder this array so that the results are score descending?
var arr = [
{ 'name': 'ashley', 'score': 5 },
{ 'name': 'jason', 'score': 9 },
{ 'name': 'ben', 'score': 1 },
{ 'name': 'jordan', 'score': 4 },
]
How can I reorder this array so that the results are score descending?
Share Improve this question edited Mar 18, 2017 at 4:15 vallentin 26.2k6 gold badges66 silver badges90 bronze badges asked Mar 18, 2017 at 4:09 TIMEXTIMEX 272k367 gold badges801 silver badges1.1k bronze badges 2- 2 Possible duplicate of stackoverflow./q/1129216/4175944 – Christopher Commented Mar 18, 2017 at 4:12
- 3 Possible duplicate of Sort array of objects by string property value in JavaScript – gyre Commented Mar 18, 2017 at 4:14
1 Answer
Reset to default 9You can do something like this:
arr.sort(function(a,b) {
return b.score - a.score
});
This gives:
[
{"name":"jason","score":9},
{"name":"ashley","score":5},
{"name":"jordan","score":4},
{"name":"ben","score":1}
]
本文标签: javascriptHow can I sort an array of dictionaries by its keyStack Overflow
版权声明:本文标题:javascript - How can I sort an array of dictionaries by its key? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741408971a2377116.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论