有没有一种更有效的方法可以对对象中的数组中的数组进行排序?

时间:2019-02-04 21:07:57

标签: javascript arrays sorting object controls

对于以下 array-object-thing 结构:

Events : {
    events : [
        {
          startDTG : {day: 0, month: 0, year: 0, time: "" },
          endDTG : {day: 0, month: 0, year: 0, time: "" },
          mode: ""
        },
        ...
    ],
    blah...,
    blah...,
    blah...
}

我正在努力寻找一种基于events键(日期时间组)对startDTG对象进行排序的更有效方法。目前,我使用以下方法,但我认为必须有一种更好的方法!

SortEvents: function() {
    this.Events.events.sort(function(a, b){return a.startDTG.time - b.startDTG.time});
    this.Events.events.sort(function(a, b){return a.startDTG.day - b.startDTG.day});
    this.Events.events.sort(function(a, b){return a.startDTG.month - b.startDTG.month});
    this.Events.events.sort(function(a, b){return a.startDTG.year - b.startDTG.year});
},

编辑1 :欲望按Year > Month > Day > Time

排序

在关键时刻,我将以需要效率的名义放弃此自定义DTG。如果需要,我可以发布整个代码,但可能没有任何意义,因为JS是为在称为“ Medialon”的专有控制系统中工作而编写的。

编辑2 :下面添加了一个快速制作的JSON代码转储,以帮助提高结构的可读性。忽略它们都是“字符串”的事实,这就是Medialon持久化字符串化的方式

{
  "events": [
    {
      "startDTG": {
        "day": "8",
        "month": "2",
        "year": "2019",
        "time": "06:35",
        "dayName": "5"
      },
      "endDTG": {
        "day": "9",
        "month": "2",
        "year": "2019",
        "time": "08:35",
        "dayName": "6"
      },
      "mode": "1"
    },
    {
      "startDTG": {
        "day": "27",
        "month": "2",
        "year": "2019",
        "time": "17:35",
        "dayName": "3"
      },
      "endDTG": {
        "day": "28",
        "month": "2",
        "year": "2019",
        "time": "06:35",
        "dayName": "4"
      },
      "mode": "1"
    },
    {
      "startDTG": {
        "day": "1",
        "month": "2",
        "year": "2019",
        "time": "14:35",
        "dayName": "5"
      },
      "endDTG": {
        "day": "2",
        "month": "2",
        "year": "2019",
        "time": "12:35",
        "dayName": "6"
      },
      "mode": "1"
    }
  ],

2 个答案:

答案 0 :(得分:1)

我仍然不太确定您的数据结构,但是这样的事情应该很接近:

const events = [
  {name: 'a', startDTG: {year: 2019, month: 1,  day: 4,  time: '14:21:46'}, endDTG: ''},
  {name: 'b', startDTG: {year: 2018, month: 10, day: 7,  time: '12:13:59'}, endDTG: ''},
  {name: 'c', startDTG: {year: 2019, month: 1,  day: 4,  time: '09:23:51'}, endDTG: ''},
  {name: 'd', startDTG: {year: 2019, month: 1,  day: 2,  time: '15:02:36'}, endDTG: ''},
  {name: 'e', startDTG: {year: 2017, month: 9,  day: 17, time: '03:25:29'}, endDTG: ''},
  {name: 'f', startDTG: {year: 2017, month: 9,  day: 17, time: '03:25:28'}, endDTG: ''},
  {name: 'g', startDTG: {year: 2018, month: 4,  day: 14, time: '11:07:42'}, endDTG: ''},
]

events.sort((
  {startDTG: {year: y1, month: m1, day: d1, time: t1}}, 
  {startDTG: {year: y2, month: m2, day: d2, time: t2}}
) => 
  // y1 - y2 || m1 - m2 || d1 - d2 || (t1 < t2 ? -1 : t1 > t2 ? 1 : 0)           
  y1 - y2 || m1 - m2 || d1 - d2 || t1.localeCompare(t2)
)

console.log(events)

答案 1 :(得分:1)

另一种解决方案是将数据映射到Date(),然后将返回的毫秒数与getTime()进行比较。

let data = {
  "events": [
    {
      "startDTG": {"day": "8", "month": "2", "year": "2019", "time": "6:35", "dayName": "5"},
      "endDTG": {"day": "9", "month": "2", "year": "2019", "time": "6:35", "dayName": "6"},
      "mode": "1"
    },
    {
      "startDTG": {"day": "27", "month": "2", "year": "2019", "time": "6:35", "dayName": "3"},
      "endDTG": {"day": "28", "month": "2", "year": "2019", "time": "6:35", "dayName": "4"},
      "mode": "1"
    },
    {
      "startDTG": {"day": "1", "month": "2", "year": "2019", "time": "6:35", "dayName": "5"},
      "endDTG": {"day": "2", "month": "2", "year": "2019", "time": "6:35", "dayName": "6"},
      "mode": "1"
    }
  ]
};

const startDTGToStr = o => `${o.year}-${o.month}-${o.day} ${o.time}`

data.events.sort((a, b) =>
{
    a = new Date(startDTGToStr(a.startDTG));
    b = new Date(startDTGToStr(b.startDTG));
    return a.getTime() - b.getTime();
});

console.log(data.events);