0

Sorry my english is not good, hope everyone understand. I have an array:

const arr=[  
  {
    name:"c",
    pay:[{
      name:"c",
      date: "2020-10-02"
    },{
      name:"cc1",
      date: "2020-10-03"
    },{
      name:"cc2",
      date: "2020-09-28"
    }]
  },{
    name:"a",
    pay:[{
      name:"aa",
      date: "2020-10-05"
    },{
      name:"aa1",
      date: "2020-10-03"
    },{
      name:"aa2",
      date: "2020-10-04"
    }]
  }, {
    name:"b",
    pay:[{
      name:"bb",
      date: "2020-10-10"
    },{
      name:"bb1",
      date: "2020-10-04"
    },{
      name:"bb2",
      date: "2020-10-01"
    }]
  }
];
Const date= new Date("2020-10-05");

I want to sort the parent element by date field. Provided that the element whose date is closest to the date variable will be first And result:

const arr=[
        {
    name:"a",
    pay:[{
      name:"aa",
      date: "2020-10-05"
    },{
      name:"aa1",
      date: "2020-10-03"
    },{
      name:"aa2",
      date: "2020-10-04"
    }]
  },{
    name:"b",
    pay:[{
      name:"bb",
      date: "2020-10-10"
    },{
      name:"bb1",
      date: "2020-10-04"
    },{
      name:"bb2",
      date: "2020-10-01"
    }]
  }, 
  {
    name:"c",
    pay:[{
      name:"c",
      date: "2020-10-02"
    },{
      name:"cc1",
      date: "2020-10-03"
    },{
      name:"cc2",
      date: "2020-09-28"
    }]
  }
]

My idea is to get the absolute value Math.abs(date- field date in aray), then the parent element with the smallest value will come first. But I have yet to deal with the logic of each sub-array. Help me please. Thanks

Cuong TQ
  • 25
  • 4
  • Does this answer your question? [How to sort an array by a date property](https://stackoverflow.com/questions/10123953/how-to-sort-an-array-by-a-date-property) – MauriceNino Oct 06 '20 at 10:32
  • that can only sort the date in the field array in the pay field. I need to sort the parent element as object. – Cuong TQ Oct 06 '20 at 10:43
  • _"But I have yet to deal with the logic of each sub-array"_ - Why exactly? Just separate the steps as you've already done in your description. First find the "closest" date in a `pay` array. Then sort `arr` as if there was only one date instead of an array of dates. Then combine the two steps. – Andreas Oct 06 '20 at 10:43

2 Answers2

0

I would create a function to do your nearest date logic, below I have called it absDate, you can then pass this to Array.sort.

eg.

const arr=[{name:"c",pay:[{name:"c",date:"2020-10-02"},{name:"cc1",date:"2020-10-03"},{name:"cc2",date:"2020-09-28"}]},{name:"a",pay:[{name:"aa",date:"2020-10-05"},{name:"aa1",date:"2020-10-03"},{name:"aa2",date:"2020-10-04"}]},{name:"b",pay:[{name:"bb",date:"2020-10-10"},{name:"bb1",date:"2020-10-04"},{name:"bb2",date:"2020-10-01"}]}];

const date = new Date("2020-10-05");

const absDate = (rec, date) => Math.min(...
    rec.pay.map(r => Math.abs(new Date(r.date) - date)));

arr.sort((a, b) => absDate(a, date) - absDate(b, date));  
  
console.log(arr);
  
Keith
  • 22,005
  • 2
  • 27
  • 44
0

You could sort the sub array based on their date and return their first entry to be used for a sort on the parent object.

const arr = [{name:"c",pay:[{name:"c",date:"2020-10-02"},{name:"cc1",date:"2020-10-03"},{name:"cc2",date:"2020-09-28"}]},{name:"a",pay:[{name:"aa",date:"2020-10-05"},{name:"aa1",date:"2020-10-03"},{name:"aa2",date:"2020-10-04"}]},{name:"b",pay:[{name:"bb",date:"2020-10-10"},{name:"bb1",date:"2020-10-04"},{name:"bb2",date:"2020-10-01"}]}];

const date = new Date("2020-10-05");

const getClosestDate = (dates, target) => {
  return dates.map(({date}) => Math.abs(new Date(date) - target)).sort((a, b) => a - b).shift();
}

const sortByDate = (arr, date) => {
  return arr.sort((a, b) => getClosestDate(a.pay, date) - getClosestDate(b.pay, date));
}

const sorted = sortByDate(arr, date);

console.log(sorted);
.as-console-wrapper { max-height: 100% !important; }
Reyno
  • 6,119
  • 18
  • 27