I have an array with following items:
[
{ collectionID: 1, collectionName: 'All Day' },
{ collectionID: 2, collectionName: 'Evening' },
{ collectionID: 3, collectionName: 'Morning' },
{ collectionID: 4, collectionName: 'Memory' },
{ collectionID: 5, collectionName: 'Brain' },
{ collectionID: 6, collectionName: 'Heart' },
{ collectionID: 7, collectionName: 'Album' },
];
I need the array output in sorted ascending order but starting three items must be at top: Output needed
[
{ collectionID: 1, collectionName: 'All Day' },
{ collectionID: 3, collectionName: 'Morning' },
{ collectionID: 2, collectionName: 'Evening' },
{ collectionID: 7, collectionName: 'Album' },
{ collectionID: 5, collectionName: 'Brain' },
{ collectionID: 6, collectionName: 'Heart' },
{ collectionID: 4, collectionName: 'Memory' },
];
I have tried this sorting of array. Getting data in sorted format but require starting three items must be on top in given format described in example.
sortArray.sort((a, b) => {
if (a.collectionName < b.collectionName)
return -1;
if (a.collectionName > b.collectionName)
return 1;
return 0;
});
But getting output
[
{ collectionID: 7, collectionName: 'Album' },
{ collectionID: 1, collectionName: 'All Day' },
{ collectionID: 5, collectionName: 'Brain' },
{ collectionID: 2, collectionName: 'Evening' },
{ collectionID: 6, collectionName: 'Heart' },
{ collectionID: 4, collectionName: 'Memory' },
{ collectionID: 3, collectionName: 'Morning' }
]
So i require the output as mentioned. collectionName All Day, Morning, Evening must be in given order and on top always followed by remaining array items in sorted format.