admin管理员组文章数量:1406052
I have a JS object as follows:
var obj = {"00:11:22:33:44:55" : "AddressB", "66:77:88:99:AA:BB" : "AddressA", "55:44:33:22:11:00" : "AddressC", "AA:BB:CC:DD:EE:FF" : "AddressD"};
The code as follows sorts it alphabetically via key:
sorted = Object.keys(obj)
.sort()
.reduce(function (accSort, keySort)
{
accSort[keySort] = obj[keySort];
return accSort;
}, {});
console.log(sorted);
Which produces the output:
{"00:11:22:33:44:55" : "AddressB", "55:44:33:22:11:00" : "AddressC", "66:77:88:99:AA:BB" : "AddressA", "AA:BB:CC:DD:EE:FF" : "AddressD"}
How can I sort the object alphabetically by value so the output is:
{"66:77:88:99:AA:BB" : "AddressA", "00:11:22:33:44:55" : "AddressB", "55:44:33:22:11:00" : "AddressC", "AA:BB:CC:DD:EE:FF" : "AddressD" }
I have a JS object as follows:
var obj = {"00:11:22:33:44:55" : "AddressB", "66:77:88:99:AA:BB" : "AddressA", "55:44:33:22:11:00" : "AddressC", "AA:BB:CC:DD:EE:FF" : "AddressD"};
The code as follows sorts it alphabetically via key:
sorted = Object.keys(obj)
.sort()
.reduce(function (accSort, keySort)
{
accSort[keySort] = obj[keySort];
return accSort;
}, {});
console.log(sorted);
Which produces the output:
{"00:11:22:33:44:55" : "AddressB", "55:44:33:22:11:00" : "AddressC", "66:77:88:99:AA:BB" : "AddressA", "AA:BB:CC:DD:EE:FF" : "AddressD"}
How can I sort the object alphabetically by value so the output is:
Share Improve this question asked Feb 1, 2021 at 20:03 rob999rob999 633 bronze badges 1{"66:77:88:99:AA:BB" : "AddressA", "00:11:22:33:44:55" : "AddressB", "55:44:33:22:11:00" : "AddressC", "AA:BB:CC:DD:EE:FF" : "AddressD" }
- 5 Although ES6 has specified the order of object properties, you generally shouldn't use objects if order is significant. Use an array. – Barmar Commented Feb 1, 2021 at 20:05
2 Answers
Reset to default 9You need to sort by the keys
by their values
first, then, use .reduce
to create the resulting ordered object:
const obj = {
"00:11:22:33:44:55": "AddressB",
"66:77:88:99:AA:BB": "AddressA",
"55:44:33:22:11:00": "AddressC",
"AA:BB:CC:DD:EE:FF": "AddressD"
};
const sorted = Object.keys(obj).sort((a,b) => obj[a].localeCompare(obj[b]))
.reduce((acc,key) => { acc[key] = obj[key]; return acc; }, {});
console.log(sorted);
Use Object.entries()
to get a 2-dimensional array of keys and values. Sort that by the values, then create the new object with reduce()
.
var obj = {"00:11:22:33:44:55" : "AddressB", "66:77:88:99:AA:BB" : "AddressA", "55:44:33:22:11:00" : "AddressC", "AA:BB:CC:DD:EE:FF" : "AddressD"};
sorted = Object.entries(obj)
.sort(([key1, val1], [key2, val2]) => val1.localeCompare(val2))
.reduce(function(accSort, [key, val]) {
accSort[key] = val;
return accSort;
}, {});
console.log(sorted);
本文标签: sortingSort Javascript object by value alphabeticallyStack Overflow
版权声明:本文标题:sorting - Sort Javascript object by value alphabetically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744966880a2635003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论