0

I am having a hard time about inner sorting of nested objects. For example I have nested array object like below.

[
  {
    "project_id": "1155091124014397",
    "completed": [
       {
          "task_due_date": "2020-06-01",      
       },
       {
          "task_due_date": "2020-20-01",      
       },
       {
          "task_due_date": "2020-02-01",      
       },
    ],
  },
  {
    "project_id": "1155091124012597",
    "completed": [
       {
          "task_due_date": "2020-22-01",      
       },
       {
          "task_due_date": "2020-05-01",      
       },
       {
          "task_due_date": "2020-01-01",      
       },
    ],
  },
]

So I am trying to sort each completed nested Array Object inside. But not to sure how to handle this. And tried something like below.

let sorted = array.map(element => {
   let nested = _.orderBy(element.completed, 'task_due_date');
   return nested
});

return sorted

But not worked out.

expected results is

[
  {
    "project_id": "1155091124014397",
    "completed": [
       {
          "task_due_date": "2020-02-01",      
       },
       {
          "task_due_date": "2020-06-01",      
       },
       {
          "task_due_date": "2020-20-01",      
       },
    ],
  },
  {
    "project_id": "1155091124012597",
    "completed": [
       {
          "task_due_date": "2020-01-01",      
       },
       {
          "task_due_date": "2020-05-01",      
       },
       {
          "task_due_date": "2020-22-01",      
       },
    ],
  },
]

Dates are sorted.

3
  • Solution depends on what expected results structure is Commented Jun 3, 2020 at 2:02
  • I added the sorted expected results @charlietfl Commented Jun 3, 2020 at 2:06
  • Change approach then and use a loop (for loop, foreach etc) and sort the completed property array in place Commented Jun 3, 2020 at 2:11

2 Answers 2

2

First, you need to convert your date strings to date objects, and then you can sort based on those objects:

const arr = [
  {
    "project_id": "1155091124014397",
    "completed": [
       {
          "task_due_date": "2020-06-01",      
       },
       {
          "task_due_date": "2020-20-01",      
       },
       {
          "task_due_date": "2020-02-01",      
       },
    ],
  },
  {
    "project_id": "1155091124012597",
    "completed": [
       {
          "task_due_date": "2020-22-01",      
       },
       {
          "task_due_date": "2020-05-01",      
       },
       {
          "task_due_date": "2020-01-01",      
       },
    ],
  },
];

const toDate = dateStr => {
  const components = dateStr.split('-');
  const [year, date, month] = components;
  return new Date(`${year}-${month}-${date}`);
};

const sortByDate = (a, b) => {
  const aDate = toDate(a['task_due_date']);
  const bDate = toDate(b['task_due_date']);
  return aDate - bDate;
};

arr.forEach(el => {
  el.completed.sort(sortByDate);
});
console.log(arr);

Sign up to request clarification or add additional context in comments.

1 Comment

@Tay no problem
0
Date.prototype.formatdate=date=>{const compo=date.split('-'); return new Date(compo[0],compo[2],compo[1])}
      const sorttime = (a,b) => {
          date=new Date()
          const  x=date.formatdate(a.task_due_date)
         const  y=date.formatdate(b.task_due_date)
        return x-y}

      arr.filter(({...rest})=>({completed:rest.completed.sort(sorttime)}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.