У меня есть многомерный массив объектов, который я хочу отсортировать по дате. Ниже я добавил один из способов, который я пытался сделать, но безуспешно. Есть ли простой способ сортировки без зацикливания?

let araaaay = [
  [{
      "dateTime": "2020-01-22T00:00:00",
      "date": "2020 01 22",
      "hour": 0,
      "postsCount": 1,
      "shift": "None"
    }, 
  ],
  [{
    "dateTime": "2020-01-19T00:00:00",
    "date": "2020 01 19",
    "hour": 0,
    "postsCount": 2,
    "shift": "A"
  }, {
    "dateTime": "2020-01-19T23:00:00",
    "date": "2020 01 19",
    "hour": 23,
    "postsCount": 2,
    "shift": "None"
  }],
  [{
    "dateTime": "2020-01-20T00:00:00",
    "date": "2020 01 20",
    "hour": 0,
    "postsCount": 0,
    "shift": "None"
  }, {
    "dateTime": "2020-01-20T01:00:00",
    "date": "2020 01 20",
    "hour": 1,
    "postsCount": 0,
    "shift": "None"
  }, {
    "dateTime": "2020-01-20T17:00:00",
    "date": "2020 01 20",
    "hour": 17,
    "postsCount": 4,
    "shift": "None"
  }, {
    "dateTime": "2020-01-20T18:00:00",
    "date": "2020 01 20",
    "hour": 18,
    "postsCount": 4,
    "shift": "None"
  } ],
  [{
      "dateTime": "2020-01-18T00:00:00",
      "date": "2020 01 18",
      "hour": 0,
      "postsCount": 0,
      "shift": "A"
    }, {
      "dateTime": "2020-01-18T03:00:00",
      "date": "2020 01 18",
      "hour": 3,
      "postsCount": 4,
      "shift": "A"
    }, {
      "dateTime": "2020-01-18T04:00:00",
      "date": "2020 01 18",
      "hour": 4,
      "postsCount": 8,
      "shift": "A"
    }, {
      "dateTime": "2020-01-18T05:00:00",
      "date": "2020 01 18",
      "hour": 5,
      "postsCount": 10,
      "shift": "B"
    }, {
      "dateTime": "2020-01-18T06:00:00",
      "date": "2020 01 18",
      "hour": 6,
      "postsCount": 8,
      "shift": "B"
    }, {
      "dateTime": "2020-01-18T07:00:00",
      "date": "2020 01 18",
      "hour": 7,
      "postsCount": 2,
      "shift": "B"
    },
    {
      "dateTime": "2020-01-18T08:00:00",
      "date": "2020 01 18",
      "hour": 8,
      "postsCount": 0,
      "shift": "B"
    },
    {
    "dateTime": "2020-01-21T22:00:00",
    "date": "2020 01 21",
    "hour": 22,
    "postsCount": 0,
    "shift": "None"
  }, {
    "dateTime": "2020-01-21T23:00:00",
    "date": "2020 01 21",
    "hour": 23,
    "postsCount": 0,
    "shift": "None"
  }],
  [{
    "dateTime": "2020-01-23T00:00:00",
    "date": "2020 01 23",
    "hour": 0,
    "postsCount": 0,
    "shift": "None"
  },  {
    "dateTime": "2020-01-23T07:00:00",
    "date": "2020 01 23",
    "hour": 7,
    "postsCount": 1,
    "shift": "None"
  },  {
    "dateTime": "2020-01-23T21:00:00",
    "date": "2020 01 23",
    "hour": 21,
    "postsCount": 1,
    "shift": "None"
  }]
];

let newArray = araaaay.sort(function(a, b) {
  return a.dateTime - b.dateTime;
});

console.log(newArray);

let newArray2 = araaaay.sort(function(a, b) {
  return a[0].dateTime - b[0].dateTime;
});

// console.log(newArray2);
2
user122222 6 Май 2020 в 23:25

2 ответа

Лучший ответ

Вам нужно использовать плоский массив перед :

const
  araaaay = 
    [ [ { dateTime: '2020-01-22T00:00:00', date: '2020 01 22', hour:  0, postsCount:  1, shift: 'None' } 
      ] 
    , [ { dateTime: '2020-01-19T00:00:00', date: '2020 01 19', hour:  0, postsCount:  2, shift: 'A'    } 
      , { dateTime: '2020-01-19T23:00:00', date: '2020 01 19', hour: 23, postsCount:  2, shift: 'None' } 
      ] 
    , [ { dateTime: '2020-01-20T00:00:00', date: '2020 01 20', hour:  0, postsCount:  0, shift: 'None' } 
      , { dateTime: '2020-01-20T01:00:00', date: '2020 01 20', hour:  1, postsCount:  0, shift: 'None' } 
      , { dateTime: '2020-01-20T17:00:00', date: '2020 01 20', hour: 17, postsCount:  4, shift: 'None' } 
      , { dateTime: '2020-01-20T18:00:00', date: '2020 01 20', hour: 18, postsCount:  4, shift: 'None' } 
      ] 
    , [ { dateTime: '2020-01-18T00:00:00', date: '2020 01 18', hour:  0, postsCount:  0, shift: 'A'    } 
      , { dateTime: '2020-01-18T03:00:00', date: '2020 01 18', hour:  3, postsCount:  4, shift: 'A'    } 
      , { dateTime: '2020-01-18T04:00:00', date: '2020 01 18', hour:  4, postsCount:  8, shift: 'A'    } 
      , { dateTime: '2020-01-18T05:00:00', date: '2020 01 18', hour:  5, postsCount: 10, shift: 'B'   } 
      , { dateTime: '2020-01-18T06:00:00', date: '2020 01 18', hour:  6, postsCount:  8, shift: 'B'    } 
      , { dateTime: '2020-01-18T07:00:00', date: '2020 01 18', hour:  7, postsCount:  2, shift: 'B'    } 
      , { dateTime: '2020-01-18T08:00:00', date: '2020 01 18', hour:  8, postsCount:  0, shift: 'B'    } 
      , { dateTime: '2020-01-21T22:00:00', date: '2020 01 21', hour: 22, postsCount:  0, shift: 'None' } 
      , { dateTime: '2020-01-21T23:00:00', date: '2020 01 21', hour: 23, postsCount:  0, shift: 'None' } 
      ] 
    , [ { dateTime: '2020-01-23T00:00:00', date: '2020 01 23', hour:  0, postsCount:  0, shift: 'None' } 
      , { dateTime: '2020-01-23T07:00:00', date: '2020 01 23', hour:  7, postsCount:  1, shift: 'None' } 
      , { dateTime: '2020-01-23T21:00:00', date: '2020 01 23', hour: 21, postsCount:  1, shift: 'None' } 
      ] ]

const aSorted = araaaay
                  .flat()
                  .sort((a,b)=>new Date(a.dateTime) - new Date(b.dateTime))

console.log (aSorted)
.as-console-wrapper { max-height: 100% !important; }
1
Mister Jojo 6 Май 2020 в 20:57

Похоже, что каждая группа имеет разные записи для одного и того же дня, но с разным временем. Это означает, что вам нужно два разных вида:

Внутренний, который сортирует объекты внутри группы (по dateTime), и внешний, который сортирует группу (по date или dateTime любого из ее элементов):

const arr = [
  [{
    "dateTime": "2020-01-22T00:00:00",
    "date": "2020 01 22",
    "hour": 0,
    "postsCount": 1,
    "shift": "None"
  }], [{
    "dateTime": "2020-01-19T00:00:00",
    "date": "2020 01 19",
    "hour": 0,
    "postsCount": 2,
    "shift": "A"
  }, {
    "dateTime": "2020-01-19T23:00:00",
    "date": "2020 01 19",
    "hour": 23,
    "postsCount": 2,
    "shift": "None"
  }], [{
    "dateTime": "2020-01-20T00:00:00",
    "date": "2020 01 20",
    "hour": 0,
    "postsCount": 0,
    "shift": "None"
  }, {
    "dateTime": "2020-01-20T01:00:00",
    "date": "2020 01 20",
    "hour": 1,
    "postsCount": 0,
    "shift": "None"
  }, {
    "dateTime": "2020-01-20T17:00:00",
    "date": "2020 01 20",
    "hour": 17,
    "postsCount": 4,
    "shift": "None"
  }, {
    "dateTime": "2020-01-20T18:00:00",
    "date": "2020 01 20",
    "hour": 18,
    "postsCount": 4,
    "shift": "None"
  }], [{
    "dateTime": "2020-01-18T00:00:00",
    "date": "2020 01 18",
    "hour": 0,
    "postsCount": 0,
    "shift": "A"
  }, {
    "dateTime": "2020-01-18T03:00:00",
    "date": "2020 01 18",
    "hour": 3,
    "postsCount": 4,
    "shift": "A"
  }, {
    "dateTime": "2020-01-18T04:00:00",
    "date": "2020 01 18",
    "hour": 4,
    "postsCount": 8,
    "shift": "A"
  }, {
    "dateTime": "2020-01-18T05:00:00",
    "date": "2020 01 18",
    "hour": 5,
    "postsCount": 10,
    "shift": "B"
  }, {
    "dateTime": "2020-01-18T06:00:00",
    "date": "2020 01 18",
    "hour": 6,
    "postsCount": 8,
    "shift": "B"
  }, {
    "dateTime": "2020-01-18T07:00:00",
    "date": "2020 01 18",
    "hour": 7,
    "postsCount": 2,
    "shift": "B"
  }, {
    "dateTime": "2020-01-18T08:00:00",
    "date": "2020 01 18",
    "hour": 8,
    "postsCount": 0,
    "shift": "B"
  }], [{
    "dateTime": "2020-01-21T22:00:00",
    "date": "2020 01 21",
    "hour": 22,
    "postsCount": 0,
    "shift": "None"
  }, {
    "dateTime": "2020-01-21T23:00:00",
    "date": "2020 01 21",
    "hour": 23,
    "postsCount": 0,
    "shift": "None"
  }], [{
    "dateTime": "2020-01-23T00:00:00",
    "date": "2020 01 23",
    "hour": 0,
    "postsCount": 0,
    "shift": "None"
  }, {
    "dateTime": "2020-01-23T07:00:00",
    "date": "2020 01 23",
    "hour": 7,
    "postsCount": 1,
    "shift": "None"
  }, {
    "dateTime": "2020-01-23T21:00:00",
    "date": "2020 01 23",
    "hour": 21,
    "postsCount": 1,
    "shift": "None"
  }],
];


// Inner sort ("by time"):
let sorted = arr.map((group) => {
  return group.sort((a, b) => {
    return new Date(a.dateTime) - new Date(b.dateTime);
  });
});

// Outer sort ("by day"):
sorted = arr.sort((a, b) => {
  return new Date(a[0].dateTime) - new Date(b[0].dateTime);
});

console.log(sorted.map(item => item.map(item => item.dateTime)))
.as-console-wrapper {
  max-height: 100% !important;
}
3
Danziger 6 Май 2020 в 20:44