3

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': [],
}
Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • 2
    You can't sort your object in that way if you want to preserve the keys as they are: integer-like object keys will always be sorted first AND numerically: https://www.stefanjudis.com/today-i-learned/property-order-is-predictable-in-javascript-objects-since-es2015/#1-integer-indices – Terry Sep 27 '20 at 22:01
  • 1
    You're talking about changing the order of keys in an object, which isn't a trivial thing; what's worse, those keys are _numeric_ (and order of those is always incremental). What you can do is either create an array from that object (where each element is essentially { key: value } pair), or maintain another array of keys ordered properly and base your view on that. – raina77ow Sep 27 '20 at 22:01
  • 1
    Just as a note, you are missing some commas in the first two code snippets. – shreyasm-dev Sep 27 '20 at 22:15
  • Would it work for you to transform the object into an array of objects? Like this [{key: "1.2.3", values: [...]}, {key: "4.4.1", values: [...]}] etc... that would mantain a strict order as you want it and it's pretty easy to do – alotropico Sep 27 '20 at 22:42

3 Answers3

2

What you are looking for is most probably:

Object.entries(obj).sort(([, a], [, b]) => b.length - a.length);

However, the output of the code above is an array consisting of enumerable property [key, value] pairs of the object again. Sorting the object's keys inside of the object itself can't be achieved.

gurisko
  • 1,172
  • 8
  • 14
1

Assuming that all keys are integers

Object.keys(obj).sort((a,b)=>a-b).reduce((acc, key)=>((acc[key]=obj[key]), acc),{});

Returns new object with sorted keys

Edit: Sorted based on value length (array length);

Object.keys(obj).sort((a,b)=>obj[b].length - obj[a].length)
    .reduce((acc, key)=>((acc[key]=obj[key]), acc),{});

const obj = {
    '1.2.3': [1,2,3,4],
    'ABC12': [],
    'CAA11': [3,5],
    '4.4.1': [1,2,3],
}
const res = Object.keys(obj)
                  .sort((a,b)=>obj[b].length - obj[a].length)
                  .reduce((acc, key)=>((acc[key]=obj[key]), acc),{});
console.log(res);
Aleks
  • 894
  • 10
  • 14
0

let obj = {
   '1.2.3': [1,2,3,4],
  'ABC12': [],
  'CAA11': [3,5],
  '4.4.1': [1,2,3],
}
console.log(obj);
let entries = Object.entries(obj).sort((a,b) => { return b[1].length - a[1].length;});
let tempObj = {};
for(let i = 0; i < entries.length; i++){
  tempObj[entries[i][0]] = entries[i][1]; 
}
obj = tempObj;
console.log(obj);
Osman Durdag
  • 955
  • 1
  • 7
  • 18