Sorry for the bad wording, I can't even formulate it in an amicable way, so I'll describe it with examples.
I have a scope of priorities:
const priorities = new Map([
['lowest', 1],
['low', 2],
['high', 3],
['highest', 4],
])
I have an array of objects with groups and priorities:
const array = [
{id: 1, priority: 'high'},
{id: 2, priority: 'lowest'},
{id: 3, priority: 'highest'},
{id: 4, group: 1, priority: 'low'},
{id: 5, group: 2, priority: 'low'},
{id: 6, group: 2, priority: 'lowest'},
{id: 7, group: 2, priority: 'low'},
{id: 8, group: 1, priority: 'high'}
];
And I have grouped this array to an object with grouped arrays of elements:
const groups = {
0: [{ id: 1, priority: 'high' }, { id: 2, priority: 'lowest' }, { id: 3, priority: 'highest' }], // have no highest priority for group 0
1: [{ id: 4, group: 1, priority: 'low' }, { id: 8, group: 1, priority: 'high' }], // high
2: [{ id: 5, group: 2, priority: 'low' }, { id: 6, group: 2, priority: 'lowest' }, { id: 7, group: 2, priority: 'low' }] // low
};
The priority of the group (except group 0) it's highest priority of the items in this group. I need to pick highest priority from an each group (except group 0) to sort groups and items from group 0 from highest to lowest, it should looks like
[<highest by priority item w/o group>, ...<items from highest by priority group>, ... , <lowest by priority items>, <>]
At all, I need sort by group's priority + item without group priority + initial position.
E.g. result for the current initial array:
const result = [
{ id: 3, priority: 'highest' },
{ id: 1, priority: 'high' },
{ id: 4, group: 1, priority: 'low' },
{ id: 8, group: 1, priority: 'high' },
{ id: 5, group: 2, priority: 'low' },
{ id: 6, group: 2, priority: 'lowest' },
{ id: 7, group: 2, priority: 'low' },
{ id: 2, priority: 'lowest' },
];
How I could make it?