I am struggling to incorporate sorting and ordering into an array.
The obstacle I am encountering involves having 5 criteria for sorting, which are:
due_date === 1000
status && is_approved(boolean)
is_overdue
(boolean checking withdate-fns
using thedue_date
)due_date
(either null, or number)is_approved
My goal is to create a sorting function that arranges the above criteria in the following order (from highest to lowest):
due_date === 1000
is_overdue
(I am utilizing isAfter of date-fns which returns a boolean)status === 'DONE' && !is_approved
due_date
starting from lowest to highest (ignore the null values)is_approved === true
any other remaining object
I was considering possibly using the .map
method and adding a ranking
value for each object by assessing the criteria, but there must be a way to accomplish this in a single iteration with the .sort
method. I have already looked at other StackOverflow threads, yet most of their data is relatively straightforward such as age, name etc.
[
{
due_date: 150000,
is_approved: false,
is_overdue: true,
status: 'TODO',
},
{
due_date: 200000,
is_approved: true,
is_overdue: false,
status: 'DONE',
},
{
due_date: 150000,
is_approved: false,
is_overdue: false,
status: 'IN PROGRESS',
},
{
due_date: 1000,
is_approved: false,
is_overdue: true,
status: 'TODO',
},
{
due_date: 200000,
is_approved: true,
is_overdue: false,
status: 'DONE',
},
{
due_date: null,
is_approved: false,
is_overdue: true,
status: 'TODO',
},
]
Which I want to convert to;
[
{
due_date: 1000, // By due_date 1000 - highest ranking
is_approved: false,
is_overdue: true,
status: 'TODO',
},
{
due_date: 150000,
is_approved: false,
is_overdue: true, // Is overdue - second highest
status: 'TODO',
},
{
due_date: null,
is_approved: false,
is_overdue: true, // Is overdue - second highest
status: 'TODO',
},
{
due_date: 200000,
is_approved: false, // Is not yet approved
is_overdue: false,
status: 'DONE', // and Is DONE
},
{
due_date: 150000,
is_approved: false,
is_overdue: false,
status: 'IN PROGRESS',
},
{
due_date: 200000, // Lowest ranked
is_approved: true, // Is APPROVED approved
is_overdue: false,
status: 'DONE', // and is DONE
},
]