admin管理员组文章数量:1325155
This is the hashmap I have
{
"LY": 43,
"US": 19,
"IN": 395,
"IR": 32,
"EG": 12,
"SA": 17,
}
How can I sort in descending order, with respect to the key values using javascript/lodash?
The expected output is:
{
"IN": 395,
"LY": 43,
"IR":32,
"US":19,
"SA":17,
"EG":12
}
This is the hashmap I have
{
"LY": 43,
"US": 19,
"IN": 395,
"IR": 32,
"EG": 12,
"SA": 17,
}
How can I sort in descending order, with respect to the key values using javascript/lodash?
The expected output is:
{
"IN": 395,
"LY": 43,
"IR":32,
"US":19,
"SA":17,
"EG":12
}
Share
Improve this question
edited Jan 22, 2016 at 6:19
Arun Mohan
asked Jan 22, 2016 at 6:09
Arun MohanArun Mohan
9784 gold badges19 silver badges39 bronze badges
1
- 2 Possible duplicate of How to sort an associative array by its values in Javascript? – djechlin Commented Jan 22, 2016 at 6:11
1 Answer
Reset to default 5Use a different data structure
You can't control the order of keys in Object
you can use an Array when it's ing to sorting data,
var obj = {
"LY": 43,
"US": 19,
"IN": 395,
"IR": 32,
"EG": 12,
"SA": 17,
};
var array = [];
for (var key in obj) {
array.push({
name: key,
value: obj[key]
});
}
var sorted = array.sort(function(a, b) {
return (a.value > b.value) ? 1 : ((b.value > a.value) ? -1 : 0)
});
本文标签: javascriptHow to sort a hashmap with respect to the valueStack Overflow
版权声明:本文标题:javascript - How to sort a hashmap with respect to the value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742153616a2423650.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论