Javascript 如何使用react根据日期对对象内的数据进行排序?

Javascript 如何使用react根据日期对对象内的数据进行排序?,javascript,reactjs,Javascript,Reactjs,我的数据结构如下所示 const items = { id: '1', Orders: [ { id: '1', title: 'Order1', startDate: "2019-08-13T00:00:00.000Z", status: 'new', } { id: '2',

我的数据结构如下所示

const items = {
    id: '1',
    Orders: [
        {
             id: '1',
             title: 'Order1',
             startDate: "2019-08-13T00:00:00.000Z",
             status: 'new',
        }
        {
             id: '2',
             title: 'Order2',
             startDate: "2020-08-13T00:00:00.000Z",
             status: 'done',
        }
    ],
    subItems: [
        {
            id: '1',
            Orders: [
                {
                    id: '1',
                    title: 'subitem-order1',
                    status: 'new',
                    startDate: '2019-08-13T00:00:00.000Z',
                }
                {
                    id: '2',
                    title: 'subitem-order2',
                    status: 'new',
                    startDate: '2020-08-13T00:00:00.000Z',
                }
            ]
        }
    ]
}
const items  = {
    orders: [
        {
             id: '2',
             title: 'Order2',
             startDate: "2020-08-13T00:00:00.000Z",
             status: 'done',
        },
        {
             id: '1',
             title: 'Order1',
             startDate: "2019-08-13T00:00:00.000Z",
             status: 'new',
         },
     ]
     subItems: [
         {
              id: '2',
              title: 'subitem-order2',
              status: 'new',
              startDate: '2020-08-13T00:00:00.000Z',
         },
         {
              id: '1',
              title: 'Order1',
              startDate: "2019-08-13T00:00:00.000Z",
              status: 'new',
         },
     ]
}
我想按startdate按升序排列订单,按startdate按升序排列子项订单

所以输出应该如下所示

const items = {
    id: '1',
    Orders: [
        {
             id: '1',
             title: 'Order1',
             startDate: "2019-08-13T00:00:00.000Z",
             status: 'new',
        }
        {
             id: '2',
             title: 'Order2',
             startDate: "2020-08-13T00:00:00.000Z",
             status: 'done',
        }
    ],
    subItems: [
        {
            id: '1',
            Orders: [
                {
                    id: '1',
                    title: 'subitem-order1',
                    status: 'new',
                    startDate: '2019-08-13T00:00:00.000Z',
                }
                {
                    id: '2',
                    title: 'subitem-order2',
                    status: 'new',
                    startDate: '2020-08-13T00:00:00.000Z',
                }
            ]
        }
    ]
}
const items  = {
    orders: [
        {
             id: '2',
             title: 'Order2',
             startDate: "2020-08-13T00:00:00.000Z",
             status: 'done',
        },
        {
             id: '1',
             title: 'Order1',
             startDate: "2019-08-13T00:00:00.000Z",
             status: 'new',
         },
     ]
     subItems: [
         {
              id: '2',
              title: 'subitem-order2',
              status: 'new',
              startDate: '2020-08-13T00:00:00.000Z',
         },
         {
              id: '1',
              title: 'Order1',
              startDate: "2019-08-13T00:00:00.000Z",
              status: 'new',
         },
     ]
}

如何根据订单的起始日期按升序对订单进行分组,以及如何根据子项的起始日期按升序对订单进行分组。有人能帮我吗。谢谢。

您可以修改
Array.prototype.sort
方法来实现这一点。将开始日期转换为以毫秒为单位的时间,并根据该时间进行排序。指定
-1和1
的顺序将为您提供ASC或DESC顺序

const项={
订单:[
{
id:'1',
标题:“订单1”,
起始日期:“2019-08-13T00:00:00.000Z”,
状态:'新',
},
{
id:'2',
标题:“订单2”,
起始日期:“2020-08-13T00:00:00.000Z”,
状态:“完成”,
}
],
分项:[
{
订单:[
{
id:'1',
标题:“子项-订单1”,
状态:'新',
起始日期:“2019-08-13T00:00:00.000Z”,
},
{
id:'2',
标题:“子项-订单2”,
状态:'新',
起始日期:“2020-08-13T00:00:00.000Z”,
}
]
}
]
}
项目.订单.排序((a,b)=>{
设TimeA=newdate(a.startDate).getTime();
设TimeB=newdate(b.startDate).getTime();
返回TimeA>TimeB-1:TimeA{
设TimeA=newdate(a.startDate).getTime();
设TimeB=newdate(b.startDate).getTime();
返回TimeA>TimeB-1:TimeA幸运的是,您的startDate是ISO datetime,因此按字母顺序排序将提供正确的时间顺序。让我们利用这个机会!
请记住,排序所依据的键是字符串,而不是数字。通过字符串键进行排序的一种方便方法是使用localeCompare,它具有获取两个字符串并返回适合排序的数字的有用功能

由于使用localeCompare进行排序很简单,因此您可以将每个列表作为一行进行排序,这允许您以易于理解的方式“构建”结果对象,如下所示:

const rawItems  = {
    orders: [
        {
             id: '2',
             title: 'Order2',
             startDate: "2020-08-13T00:00:00.000Z",
             status: 'done',
        },
        {
             id: '1',
             title: 'Order1',
             startDate: "2019-08-13T00:00:00.000Z",
             status: 'new',
         },
     ],
     subItems: [
         {
              id: '2',
              title: 'subitem-order2',
              status: 'new',
              startDate: '2020-08-13T00:00:00.000Z',
         },
         {
              id: '1',
              title: 'Order1',
              startDate: "2019-08-13T00:00:00.000Z",
              status: 'new',
         },
     ]
}

const items = {
    orders: rawItems.orders.sort(
       (order1,order2) => order1.startDate.localeCompare(order2.startDate)
    ),
    subitems: rawItems.subItems.sort(
       (sub1,sub2) => sub1.startDate.localeCompare(sub2.startDate)
    ),

}

console.log(items)
       (order1,order2) => -order1.startDate.localeCompare(order2.startDate)
在问题中,您反复指定“升序”,即先小值,然后大值。然而,您给出的示例答案是降序的

我提供的示例答案没有按照您指定的升序。如果您的意思是“降序”,那么只需按如下方式对
localeCompare
值求反:

const rawItems  = {
    orders: [
        {
             id: '2',
             title: 'Order2',
             startDate: "2020-08-13T00:00:00.000Z",
             status: 'done',
        },
        {
             id: '1',
             title: 'Order1',
             startDate: "2019-08-13T00:00:00.000Z",
             status: 'new',
         },
     ],
     subItems: [
         {
              id: '2',
              title: 'subitem-order2',
              status: 'new',
              startDate: '2020-08-13T00:00:00.000Z',
         },
         {
              id: '1',
              title: 'Order1',
              startDate: "2019-08-13T00:00:00.000Z",
              status: 'new',
         },
     ]
}

const items = {
    orders: rawItems.orders.sort(
       (order1,order2) => order1.startDate.localeCompare(order2.startDate)
    ),
    subitems: rawItems.subItems.sort(
       (sub1,sub2) => sub1.startDate.localeCompare(sub2.startDate)
    ),

}

console.log(items)
       (order1,order2) => -order1.startDate.localeCompare(order2.startDate)


这回答了你的问题吗?