I have an object of key (str): value (arr) pair.
const obj = {
'1': []
'2': ['a', 'b'],
'3': [],
'4': ['c']
}
I would like to sort this by the number of elements that each key has in the value.
So that it would be like,
const obj = {
'2': ['a', 'b'],
'4': ['c']
'1': []
'3': [],
}
After some searching, I tried this syntax
const sortable = Object.fromEntries(
Object.entries(obj).sort(([,a],[,b]) => b.length)
);
But this gives me undefined.
Any help?
EDIT
Sorry the keys are String
More detailed example
const obj = {
'1.2.3': [1,2,3,4],
'ABC12': [],
'CAA11': [3,5],
'4.4.1': [1,2,3],
}
to
const obj = {
'1.2.3': [1,2,3,4],
'4.4.1': [1,2,3],
'CAA11': [3,5],
'ABC12': [],
}