I need assistance with sorting an array of objects based on a status value from the backend system. The sorting priority should follow a specific order defined in the priority array:
const priority = [
"APPROVED",
"WITHDRAW_PENDING",
"PENDING",
"WITHDRAWN",
"CANCELLED",
"REJECTED",
]
Currently, my sorting function looks like this:
getActiveEnrolment(enrolments) {
console.log(enrolments)
const enrolmentsFiltered = enrolments.sort((a, b) => priority.indexOf(a.status) > priority.indexOf(b.status));
console.log(enrolmentsFiltered);
}
An example of the enrolments object being used:
{enrollable: {id: "fb9de5ae-2b13-49ce-ac58-e82db55c078b", itemdata: {…}, itemtype: "Course"}
id: "e66a34cd-1889-48ba-9a86-42eb87dfe86e"
status: "APPROVED"
user: "4fd79b04-9ed0-4942-84fc-9670f8a89050"}
Despite implementing the sort function, the array is not getting sorted as expected. It seems to be returning the same array as the original enrolments array. I'm unsure what could be causing this issue. Please note that the enrolments array may not necessarily include all the statuses listed in the priority array and can have duplicate statuses.
console.log
:
(3) [{…}, {…}, {…}]
0: {id: "e66a34cd-1889-48ba-9a86-42eb87dfe86e", user: "4fd79b04-9ed0-4942-84fc-9670f8a89050", enrollable: {…}, status: "APPROVED"}
1: {id: "a445d96a-3bac-42db-b4a8-682076dfc5a7", user: "4fd79b04-9ed0-4942-84fc-9670f8a89050", enrollable: {…}, status: "REJECTED"}
2: {id: "16c5f940-3640-4d75-92d5-46a1f2257a02", user: "4fd79b04-9ed0-4942-84fc-9670f8a89050", enrollable: {…}, status: "PENDING"}
length: 3
__proto__: Array(0)
(3) [{…}, {…}, {…}]
0: {id: "e66a34cd-1889-48ba-9a86-42eb87dfe86e", user: "4fd79b04-9ed0-4942-84fc-9670f8a89050", enrollable: {…}, status: "APPROVED"}
1: {id: "a445d96a-3bac-42db-b4a8-682076dfc5a7", user: "4fd79b04-9ed0-4942-84fc-9670f8a89050", enrollable: {…}, status: "REJECTED"}
2: {id: "16c5f940-3640-4d75-92d5-46a1f2257a02", user: "4fd79b04-9ed0-4942-84fc-9670f8a89050", enrollable: {…}, status: "PENDING"}