Here is my dictionary:
const dict = {
"key_1" : "z",
"key_2" : "a",
"key_3" : "b",
"key_4" : "y"
};
I want to sort it alphabetically by value so it looks like this:
const sorted_dict = {
"key_2" : "a",
"key_3" : "b",
"key_4" : "y",
"key_1" : "z"
};
This is what I think should work:
var items = Object.keys(dict).map(function(key) {
return [key, dict[key]];
});
items.sort((a, b) => a[1] - b[1]);
console.log(items)
But it's not sorting at all:
[
[
"key_1",
"z"
],
[
"key_2",
"a"
],
[
"key_3",
"b"
],
[
"key_4",
"y"
]
]
Why is the sorting not working?