不起作用的对象数组的排序

时间:2018-03-15 17:33:14

标签: javascript node.js reactjs

在React中,我有一个带有createdAt属性的对象数组,我试图对其进行排序。然而,排序看起来似乎没有正常工作,因为有些它的顺序错误。我在Node.js中编写了以下代码用于测试...任何帮助都将不胜感激。

const moment = require('./node_modules/moment/moment.js')

const sortObjects = () => {
  const objects = [
    {id: "cje633i3v03wl0130lsse3zev", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cjeqtudhd000u0149skzacpq5", createdAt: "2018-03-14T08:31:57.000Z"},
    {id: "cje633goc03vn01309m2iocas", createdAt: "2018-02-27T20:07:48.000Z"},
    {id: "cje633k1z03ww0130ce27niez", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633k3303x50130n1a7vnft", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633k5b03xa0130m07ndgpn", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633k2y03x10130q1076pq4", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633k0b03wq0130bmg0t6rd", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633gmp03ve0130xu314ti4", createdAt: "2018-02-27T20:07:47.000Z"},
    {id: "cje633i2x03wd0130cqk5sdap", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cje633i1w03w60130rfpngz0b", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cje633i1u03w301307s44jfyy", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cje633i3i03wh0130f3t1iyl4", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cje633gp803vw0130frck18wq", createdAt: "2018-02-27T20:07:48.000Z"},
    {id: "cje633gos03vs0130tzv7xfpe", createdAt: "2018-02-27T20:07:48.000Z"},
    {id: "cje633gnu03vj0130nnt4abin", createdAt: "2018-02-27T20:07:47.000Z"},
    {id: "cje633enx03uo0130qw0r35l6", createdAt: "2018-02-27T20:07:45.000Z"},
    {id: "cje633eot03uw0130r42aqbox", createdAt: "2018-02-27T20:07:45.000Z"},
    {id: "cje633eou03uy0130hdwg0uvn", createdAt: "2018-02-27T20:07:45.000Z"},
    {id: "cje633epl03v30130hsugr6vp", createdAt: "2018-02-27T20:07:45.000Z"},
    {id: "cje633eps03v70130xr826vf2", createdAt: "2018-02-27T20:07:45.000Z"},
  ]
  objects.sort( (a, b) => moment.utc(b.createdAt).isAfter(moment.utc(a.createdAt)));
  console.log('objects',objects)
}

sortObjects()

更新 - 输出

[ { id: 'cje633i1w03w60130rfpngz0b', createdAt: '2018-02-27T20:07:49.000Z' },
   { id: 'cje633i3v03wl0130lsse3zev', createdAt: '2018-02-27T20:07:49.000Z' },
   { id: 'cje633i3i03wh0130f3t1iyl4', createdAt: '2018-02-27T20:07:49.000Z' },
   { id: 'cje633k1z03ww0130ce27niez', createdAt: '2018-02-27T20:07:52.000Z' },
   { id: 'cje633k3303x50130n1a7vnft', createdAt: '2018-02-27T20:07:52.000Z' },
   { id: 'cje633k5b03xa0130m07ndgpn', createdAt: '2018-02-27T20:07:52.000Z' },
   { id: 'cje633k2y03x10130q1076pq4', createdAt: '2018-02-27T20:07:52.000Z' },
   { id: 'cje633k0b03wq0130bmg0t6rd', createdAt: '2018-02-27T20:07:52.000Z' },
   { id: 'cje633i1u03w301307s44jfyy', createdAt: '2018-02-27T20:07:49.000Z' },
   { id: 'cje633i2x03wd0130cqk5sdap', createdAt: '2018-02-27T20:07:49.000Z' },
   { id: 'cjeqtudhd000u0149skzacpq5', createdAt: '2018-03-14T08:31:57.000Z' },
   { id: 'cje633goc03vn01309m2iocas', createdAt: '2018-02-27T20:07:48.000Z' },
   { id: 'cje633gp803vw0130frck18wq', createdAt: '2018-02-27T20:07:48.000Z' },
   { id: 'cje633gos03vs0130tzv7xfpe', createdAt: '2018-02-27T20:07:48.000Z' },
   { id: 'cje633gmp03ve0130xu314ti4', createdAt: '2018-02-27T20:07:47.000Z' },
   { id: 'cje633gnu03vj0130nnt4abin', createdAt: '2018-02-27T20:07:47.000Z' },
   { id: 'cje633enx03uo0130qw0r35l6', createdAt: '2018-02-27T20:07:45.000Z' },
   { id: 'cje633eot03uw0130r42aqbox', createdAt: '2018-02-27T20:07:45.000Z' },
   { id: 'cje633eou03uy0130hdwg0uvn', createdAt: '2018-02-27T20:07:45.000Z' },
   { id: 'cje633epl03v30130hsugr6vp', createdAt: '2018-02-27T20:07:45.000Z' },
   { id: 'cje633eps03v70130xr826vf2', createdAt: '2018-02-27T20:07:45.000Z' } ]

5 个答案:

答案 0 :(得分:3)

you should return a number时,您将从排序回调中返回logger.info("Info test")true。除此之外,您不需要false按这些日期排序。你可以使用:

moment.js

按升序排序,或翻转objects.sort( (a, b) => new Date(a.createdAt) - new Date(b.createdAt)); a按降序排序。

答案 1 :(得分:1)

您可以将ISO 8601日期字符串视为叮咬。它们可以在不使用日期方法的情况下进行排序,只要它们不是同一时区(实际上是zulu(UTC))。

const objects = [{ id: "cje633i3v03wl0130lsse3zev", createdAt: "2018-02-27T20:07:49.000Z" }, { id: "cjeqtudhd000u0149skzacpq5", createdAt: "2018-03-14T08:31:57.000Z" }, { id: "cje633goc03vn01309m2iocas", createdAt: "2018-02-27T20:07:48.000Z" }, { id: "cje633k1z03ww0130ce27niez", createdAt: "2018-02-27T20:07:52.000Z" }, { id: "cje633k3303x50130n1a7vnft", createdAt: "2018-02-27T20:07:52.000Z" }, { id: "cje633k5b03xa0130m07ndgpn", createdAt: "2018-02-27T20:07:52.000Z" }, { id: "cje633k2y03x10130q1076pq4", createdAt: "2018-02-27T20:07:52.000Z" }, { id: "cje633k0b03wq0130bmg0t6rd", createdAt: "2018-02-27T20:07:52.000Z" }, { id: "cje633gmp03ve0130xu314ti4", createdAt: "2018-02-27T20:07:47.000Z" }, { id: "cje633i2x03wd0130cqk5sdap", createdAt: "2018-02-27T20:07:49.000Z" }, { id: "cje633i1w03w60130rfpngz0b", createdAt: "2018-02-27T20:07:49.000Z" }, { id: "cje633i1u03w301307s44jfyy", createdAt: "2018-02-27T20:07:49.000Z" }, { id: "cje633i3i03wh0130f3t1iyl4", createdAt: "2018-02-27T20:07:49.000Z" }, { id: "cje633gp803vw0130frck18wq", createdAt: "2018-02-27T20:07:48.000Z" }, { id: "cje633gos03vs0130tzv7xfpe", createdAt: "2018-02-27T20:07:48.000Z" }, { id: "cje633gnu03vj0130nnt4abin", createdAt: "2018-02-27T20:07:47.000Z" }, { id: "cje633enx03uo0130qw0r35l6", createdAt: "2018-02-27T20:07:45.000Z" }, { id: "cje633eot03uw0130r42aqbox", createdAt: "2018-02-27T20:07:45.000Z" }, { id: "cje633eou03uy0130hdwg0uvn", createdAt: "2018-02-27T20:07:45.000Z" }, { id: "cje633epl03v30130hsugr6vp", createdAt: "2018-02-27T20:07:45.000Z" }, { id: "cje633eps03v70130xr826vf2", createdAt: "2018-02-27T20:07:45.000Z" }];

objects.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
  
console.log(objects);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

var sortedObj = objects.sort(function(a,b){
    return new Date(b.createdAt) - new Date(a.createdAt);
});

console.log(sortedObj)

答案 3 :(得分:0)

将它们作为字符串进行比较。 a.createdAt < b.createdAt? 1 : -1将降序排序。 a.createdAt > b.createdAt? 1 : -1 sill排序上升

const sortObjects = () => {
  const objects = [
    {id: "cje633i3v03wl0130lsse3zev", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cjeqtudhd000u0149skzacpq5", createdAt: "2018-03-14T08:31:57.000Z"},
    {id: "cje633goc03vn01309m2iocas", createdAt: "2018-02-27T20:07:48.000Z"},
    {id: "cje633k1z03ww0130ce27niez", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633k3303x50130n1a7vnft", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633k5b03xa0130m07ndgpn", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633k2y03x10130q1076pq4", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633k0b03wq0130bmg0t6rd", createdAt: "2018-02-27T20:07:52.000Z"},
    {id: "cje633gmp03ve0130xu314ti4", createdAt: "2018-02-27T20:07:47.000Z"},
    {id: "cje633i2x03wd0130cqk5sdap", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cje633i1w03w60130rfpngz0b", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cje633i1u03w301307s44jfyy", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cje633i3i03wh0130f3t1iyl4", createdAt: "2018-02-27T20:07:49.000Z"},
    {id: "cje633gp803vw0130frck18wq", createdAt: "2018-02-27T20:07:48.000Z"},
    {id: "cje633gos03vs0130tzv7xfpe", createdAt: "2018-02-27T20:07:48.000Z"},
    {id: "cje633gnu03vj0130nnt4abin", createdAt: "2018-02-27T20:07:47.000Z"},
    {id: "cje633enx03uo0130qw0r35l6", createdAt: "2018-02-27T20:07:45.000Z"},
    {id: "cje633eot03uw0130r42aqbox", createdAt: "2018-02-27T20:07:45.000Z"},
    {id: "cje633eou03uy0130hdwg0uvn", createdAt: "2018-02-27T20:07:45.000Z"},
    {id: "cje633epl03v30130hsugr6vp", createdAt: "2018-02-27T20:07:45.000Z"},
    {id: "cje633eps03v70130xr826vf2", createdAt: "2018-02-27T20:07:45.000Z"},
  ]
  objects.sort( (a, b) =>a.createdAt < b.createdAt? 1 : -1);
  console.log('objects',objects)
}

sortObjects()

答案 4 :(得分:0)

我可以向您确认,由于Javascript使用的不稳定排序算法,您必须返回3个可能的值: 1,0和-1。具有相同值的元素的排序顺序不是保证。 因此,正确的排序函数调用可以(在纯JavaScript中)

Table2