Given
a = [{"a": 1}, {"b": 0}, {"w", -4}]
I want to rearrange this into
a = [{"w", -4},{"b": 0}, {"a": 1}]
Sort by lowest to greatest
Trying to do this with a.sort()
Given
a = [{"a": 1}, {"b": 0}, {"w", -4}]
I want to rearrange this into
a = [{"w", -4},{"b": 0}, {"a": 1}]
Sort by lowest to greatest
Trying to do this with a.sort()
You could use sort() and Object.values
a = [{"a": 1}, {"b": 0}, {"w": -4}]
a.sort((a,b)=>Object.values(a)[0]-Object.values(b)[0])
console.log(a)
Couln't you do it by passing by a map?
Maybe :
var map = new Map();
map.set("a", 1);
map.set("b", 0);
map.set("w", -4);
const sortedMapByValue= new Map([...map.entries()].sort((a, b) => a[1] - b[1]));
That normally gives you the result you want when iterating on sortedMapByValue
Edit: I haven't seen latest answer, pretty similar, better if you want to keep your array as is it
You could use sort with a custom comparator like so:
a.sort(( a, b, o=Object.values ) => o(a)[0] - o(b)[0] )