Updated requirement on 04/06/2021:
I have two array of objects, arrX and arrY. Need to sort the objects of 'arrY' same as the order of 'arrX'. What is the shortest or best way?
Note: Objects which has type other than types of arrX should got to the bottom ie. "type: 'X'" here.
const arrX = [
{type: 'C', category: 'CAT2'},
{type: 'A', category: 'CAT1},
{type: 'B', category: 'CAT3'},
]
const arrY = [
{type: 'B', category: 'CAT3'},
{type: 'A', category: 'CAT1'},
{type: 'C', category: 'CAT2'},
{type: 'B', category: 'CAT3'},
{type: 'A', category: 'CAT1'},
{type: 'X', category: 'CAT4'},
{type: 'B', category: 'CAT2'},
{type: 'X', category: 'CAT4'},
{type: 'X', category: 'CAT5'},
{type: 'A', category: 'CAT1'},
{type: 'C', category: 'CAT2'},
]
Should Be sorted as:
const arrX = [
{type: 'C', category: 'CAT2'},
{type: 'C', category: 'CAT2'},
{type: 'A', category: 'CAT1'},
{type: 'A', category: 'CAT1'},
{type: 'A', category: 'CAT1'},
{type: 'B', category: 'CAT3'},
{type: 'B', category: 'CAT3'},
{type: 'X', category: 'CAT4'},
{type: 'B', category: 'CAT2'},
{type: 'X', category: 'CAT4'},
{type: 'X', category: 'CAT5'},
]