为了账号安全,请及时绑定邮箱和手机立即绑定

在具有 2 个日期的脚本中对 json 数据进行排序

在具有 2 个日期的脚本中对 json 数据进行排序

米琪卡哇伊 2022-09-23 21:33:16
我有以下 json 数据:data = [  {    stateCode: 'CH',    startDate: new Date('06/12/2020 08:00'),    startDateWithDelay: new Date('06/12/2020 08:00'),  },  {    stateCode: 'LA',    startDate: new Date('06/12/2020 08:00'),    startDateWithDelay: new Date('06/12/2020 08:30'),  },  {    stateCode: 'NY',    startDate: new Date('06/12/2020 06:00'),    startDateWithDelay: new Date('06/12/2020 06:00')  }]默认情况下,数据应按起始日期排序。但是,如果 2 条记录的开始日期相同,则应按状态日期与Delay 属性进行排序。在上面的示例中,前 2 条记录的开始日期是相同的。因此,在这种情况下,应根据“开始日期”属性对其进行排序。排序后,结果应根据 2 个日期显示以下状态:纽约 纽约 芝加哥CH 洛杉矶 洛杉矶我正在使用以下代码根据开始日期进行排序import { sortBy } from 'lodash-es'data = data.sortBy(data, (obj) => obj.startDate);当前 2 条记录的启动日期相同时,如何按“开始日期”属性完成排序。
查看完整描述

4 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

您可以使用传递给 的自定义排序函数基于两个参数进行排序Array.prototype.sort


const data = [

  {

    stateCode: 'CH',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:00'),

  },

  {

    stateCode: 'LA',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:30'),

  },

  {

    stateCode: 'NY',

    startDate: new Date('06/12/2020 06:00'),

    startDateWithDelay: new Date('06/12/2020 06:00')

  }

]


const sorted = data.sort((a, b) => {

  const startDateComparison = a.startDate - b.startDate

  if (startDateComparison !== 0) return startDateComparison

  return a.startDateWithDelay - b.startDateWithDelay

})


console.log(sorted)


查看完整回答
反对 回复 2022-09-23
?
守着一只汪

TA贡献1872条经验 获得超3个赞

最好的方法是使用 as, 函数,使我们能够灵活地覆盖任何类型的数组数据的数组的排序行为,并且可以用于自定义属性的自定义排序顺序。看看我举的最后一个例子。Array.prototype.sortArray.prototype.sort


const data = [

  {

    stateCode: "CH",

    startDate: new Date("06/12/2020 08:00"),

    startDateWithDelay: new Date("06/12/2020 08:00"),

  },

  {

    stateCode: "LA",

    startDate: new Date("06/12/2020 08:00"),

    startDateWithDelay: new Date("06/12/2020 08:30"),

  },

  {

    stateCode: "NY",

    startDate: new Date("06/12/2020 06:00"),

    startDateWithDelay: new Date("06/12/2020 06:00"),

  },

];


const sorted = data.sort((a, b) => {

  // one liner using conditional operator

  return a.startDate - b.startDate === 0

    ? a.startDateWithDelay - b.startDateWithDelay

    : a.startDate - b.startDate;

});


console.log(sorted);

在同一示例中,如果我们必须按“LA”,“NY”和“CH”的顺序对状态进行排序,那么我们也可以这样做,如下面的示例所示。


const data = [

  {

    stateCode: "CH",

    startDate: new Date("06/12/2020 08:00"),

    startDateWithDelay: new Date("06/12/2020 08:00"),

  },

  {

    stateCode: "LA",

    startDate: new Date("06/12/2020 08:00"),

    startDateWithDelay: new Date("06/12/2020 08:30"),

  },

  {

    stateCode: "NY",

    startDate: new Date("06/12/2020 06:00"),

    startDateWithDelay: new Date("06/12/2020 06:00"),

  },

];


// Custom Sorting based on States in order of 'LA', 'NY' & 'CH'

const sorted = data.sort((a, b) => {

  const sCa = a.stateCode; //state code of a

  const sCb = b.stateCode; //state code of b

  return (sCa === 'LA' && sCb !== 'LA') || (sCa === 'NY' && sCb === 'CH') ? -1 : 

  (sCb === 'LA' && sCa !== 'LA') || (sCb === 'NY' && sCa === 'CH') ? 1 : 0;

});


console.log(sorted);


查看完整回答
反对 回复 2022-09-23
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

data = [

  {

    stateCode: 'CH',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:00'),

  },

  {

    stateCode: 'LA',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:30'),

  },

  {

    stateCode: 'NY',

    startDate: new Date('06/12/2020 06:00'),

    startDateWithDelay: new Date('06/12/2020 06:00')

  }

]

我们可以使用数组的内置排序函数,因此我们将比较 startDate 如果它们相同,我们将在 startDate 上排序,否则继续在 startDate 上排序。


let data2 = data.sort((s1, s2) => s1.startDate == s2.startDate ? 

            s1.startDateWithDelay - s2.startDateWithDelay : 

            s1.startDate - s2.startDate); 

排序完成后,我们可以使用map来修改结果,并加入预期输出的排序状态列表。


let states = data2.map(s => s.stateCode).join(', ');

这将输出预期的结果。


NY, CH, LA


查看完整回答
反对 回复 2022-09-23
?
12345678_0001

TA贡献1802条经验 获得超5个赞

你可以做这样的事情


data = [

  {

    stateCode: 'CH',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:00'),

  },

  {

    stateCode: 'LA',

    startDate: new Date('06/12/2020 08:00'),

    startDateWithDelay: new Date('06/12/2020 08:30'),

  },

  {

    stateCode: 'NY',

    startDate: new Date('06/12/2020 06:00'),

    startDateWithDelay: new Date('06/12/2020 06:00')

  }

];


data.sort((a,b)=>{

 if(a.startDate.getTime() < b.startDate.getTime()){

  return -1

 }else if(a.startDate.getTime() < b.startDate.getTime()){

  return 1

 }else{

  return a.startDateWithDelay.getTime() - b.startDateWithDelay.getTime()

 }

});


console.log(data)


查看完整回答
反对 回复 2022-09-23
  • 4 回答
  • 0 关注
  • 81 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信