Support
Quality
Security
License
Reuse
kandi has reviewed date-fns and discovered the below as its top functions. This is intended to give you an instant insight into date-fns implemented functionality, and help decide if they suit your requirements.
Get all kandi verified functions for this library.
Get all kandi verified functions for this library.
⏳ Modern JavaScript date utility library ⌛️
See all related Code Snippets
QUESTION
Sorting and ordering an array with different criteria
Asked 2022-Apr-08 at 12:05I am trying to implement sorting and order to an array.
The challenge which I am facing is that I have 5 criteria on which to base my sorting, which are:
due_date === 1000
status && is_approved(boolean)
is_overdue
(boolean checking with
date-fns
using the due_date
)due_date
(either null, or number)is_approved
What I would like to achieve is to create a sorting function which orders the above criteria as the following (from highest to lowest):
due_date === 1000
is_overdue
(I am using isAfter of the 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 thinking of perhaps running .map
method, and appending a ranking
value for each of the object by checking the criteria, but there must be a way of doing this in single iteration with .sort
method. I already checked other StackOverflow threads, but most of their data is relatively simple 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 transform 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
},
]
ANSWER
Answered 2022-Apr-08 at 11:32You just need to define a sorting function
I've implemented the first two requirements I leave the rest to you
Probably it needs a bit of refactoring but you get the idea
const data = [
{
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',
},
]
const sortingFn = (a, b) => {
if(a.due_date === 1000){
return -1
}
if(b.due_date === 1000){
return 1
}
if(a.is_overdue){
return -1
}
if(b.is_overdue){
return 1
}
//the rest of the conditions
}
data.sort(sortingFn)
console.log(data)
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
See Similar Libraries in
Save this library and start creating your kit
Open Weaver – Develop Applications Faster with Open Source