I have this array:
arr = [
{ name: 'This one', number: '67', codes:['B33', '45']},
{ name: 'Another', number: '003', codes: ['55', 'A47']},
{ name: 'Something', codes:['A33']},
{ name: 'One more', number: '003'},
{ name: 'Anything', number: '67', codes:['A33']},
];
I want to order it first by "number" and then by "codes" in in ascending order, but both are not required and "codes" is an array of string.
The result should be:
arr = [
{ name: 'Something', codes:['A33']},
{ name: 'One more', number: '003'},
{ name: 'Another', number: '003', codes: ['55', 'A47']},
{ name: 'Anything', number: '67', codes:['A33']},
{ name: 'This one', number: '67', codes:['B33', '45']}
];
Where the object that doesn't have number
must be treated as number: ''
and the object that doesn't have codes
must be treated as codes : ''
.
codes
is as an array of string. It should be compared like a unique string. For example: codes: ['55', 'A47']
must be compared like codes: '55, A47'
I tried to follow this code bellow, but it's not working for my case:
function orderIt() {
return arr.sort((a, b) =>
a.number - b.number || a.codes - b.codes
);
}
(source)
Explaining why my question is different: In the other questions, both are required and there aren't an array of string inside of it.