I have a object wit values (name)
So now i want to sort this array by value.
Here is what i got:
admin_table_id' => [
'2' => 'blogs',
'15' => 'users',
'18' => 'users_rights',
'19' => 'didyouknow',
'30' => 'admin_table',
'37' => 'privacy'
]
and i want something like:
'admin_table_id' => [
'30' => 'admin_table',
'2' => 'blogs',
'19' => 'didyouknow',
'37' => 'privacy'
'15' => 'users',
'18' => 'users_rights',
]
i tried different sort functions but he doesn’t sort by value
I need this for a <select> field in a vue component
the code i tried is here:
sortedOptions() {
if (!this.options || !Array.isArray(this.options)) return [];
const sorted = this.options.map(obj => {
return Object.fromEntries(
Object.entries(obj).map(([outerKey, innerObj]) => {
const rawInnerObj = innerObj;
const sortedInner = Object.fromEntries(
Object.entries(rawInnerObj)
.sort(([, a], [, b]) => {
if (typeof a === 'string' && typeof b === 'string') {
return a.localeCompare(b);
} else if (typeof a === 'number' && typeof b === 'number') {
return a - b;
}
return 0;
})
);
return [outerKey, sortedInner];
})
);
});
nextTick(() => {
console.log("Nach der Sortierung:", JSON.stringify(sorted, null, 2));
});
return sorted;
}