在迭代之后停止按日期值对JSON数组进行排序

时间:2019-05-25 23:25:05

标签: javascript arrays json date object

因此,我正在尝试通过日期键对JSON数组进行排序,目前的问题似乎是该函数在进行了一种排序后就停止了(或者是很明显的错误)。

以下是我的排序js

  function sortByDate() {
        result = gloresult
        var newA = result.sort(function(a,b){
          return Number(new Date(a.Date)) - Number(new Date(b.Date));
        });

       console.log(newA)


      }

输入Json文件

gloresult = [
    {
        "Heading": "A",
        "Topic A": "Ball Valve",
        "Date": "2/05/2019"
    },
    {
        "Heading": "B",
        "Topic A": "ABS",
        "Date": "1/05/2019"
    },
    {
        "Heading": "C",
        "Topic A": "Acrylic",
        "Date": "21/05/2019"
    },
    {
        "Heading": "D",
        "Topic A": "Adaptor Fitting",
        "Date": "21/05/2019"
    },
    {
        "Heading": "E",
        "Topic A": "Air Gap",
        "Date": "4/05/2019"
    },
    {
        "Heading": "F",
        "Topic A": "Stuff",
        "Date": "21/03/2019"
    },
    {
        "Heading": "G",
        "Topic A": "Stuff",
        "Date": "21/04/2019"
    },
    {
        "Heading": "H",
        "Topic A": "Stuff",
        "Date": "21/05/2021"
    }

]

输出Json文件

[
    {
        "Heading": "B",
        "Topic A": "ABS",
        "Date": "1/05/2019"

    },
    {
        "Heading": "A",
        "Topic A": "Ball Valve",
        "Date": "2/05/2019"

    },
    {
        "Heading": "C",
        "Topic A": "Acrylic",
        "Date": "21/05/2010"

    },
    {
        "Heading": "D",
        "Topic A": "Adaptor Fitting",
        "Date": "21/05/2019"

    },
    {
        "Heading": "E",
        "Topic A": "Air Gap",
        "Date": "4/05/2019"

    },
    {
        "Heading": "F",
        "Topic A": "Stuff",
        "Date": "21/03/2019"

    },
    {
        "Heading": "G",
        "Topic A": "Stuff",
        "Date": "21/04/2019"

    },
    {
        "Heading": "H",
        "Topic A": "Stuff",
        "Date": "21/05/2021"
    }
]

如您所见,只有A和B更改了位置,结果仍然完全相同。我不确定这是否是因为用户在html页面上选择按钮时正在调用该函数。

2 个答案:

答案 0 :(得分:3)

因为日期采用DD/MM/YYYY格式。 Date构造函数期望使用MM/DD/YYYY格式。 (采用DD/MM/YYYY格式的新日期,如果无效,则传递给NaN时将产生Number)。只需在函数中稍微更改代码即可解决此问题:

const gloresult = [{"Heading":"A","Topic A":"Ball Valve","Date":"2/05/2019"},{"Heading":"B","Topic A":"ABS","Date":"1/05/2019"},{"Heading":"C","Topic A":"Acrylic","Date":"21/05/2019"},{"Heading":"D","Topic A":"Adaptor Fitting","Date":"21/05/2019"},{"Heading":"E","Topic A":"Air Gap","Date":"4/05/2019"},{"Heading":"F","Topic A":"Stuff","Date":"21/03/2019"},{"Heading":"G","Topic A":"Stuff","Date":"21/04/2019"},{"Heading":"H","Topic A":"Stuff","Date":"21/05/2021"}];

const sortByDate = () => {
  let result = gloresult;
  let newA = result.sort(({ Date: a }, { Date: b }) => {
    let [da, ma, ya] = a.split("/");
    let [db, mb, yb] = b.split("/");
    return Number(new Date([ma, da, ya].join("/"))) - Number(new Date([mb, db, yb].join("/")));
  });
  console.log(newA);
}

sortByDate();
.as-console-wrapper { max-height: 100% !important; top: auto; }

答案 1 :(得分:0)

这有点棘手,但是有效!

var
  gloresult = [ { "Heading": "A", "Topic A": "Ball Valve",      "Date": "2/05/2019"   }
              , { "Heading": "B", "Topic A": "ABS",             "Date": "1/05/2019"   }
              , { "Heading": "C", "Topic A": "Acrylic",         "Date": "21/05/2019"  }
              , { "Heading": "D", "Topic A": "Adaptor Fitting", "Date": "21/05/2019"  }
              , { "Heading": "E", "Topic A": "Air Gap",         "Date": "4/05/2019"   }
              , { "Heading": "F", "Topic A": "Stuff",           "Date": "21/03/2019"  }
              , { "Heading": "G", "Topic A": "Stuff",           "Date": "21/04/2019"  }
              , { "Heading": "H", "Topic A": "Stuff",           "Date": "21/05/2021"  }
              ];

gloresult.sort((a, b)=>{
  let
    aa = a.Date.split('/').reverse().map(d=>('0000'+d).slice(-4)).join('-'),
    bb = b.Date.split('/').reverse().map(d=>('0000'+d).slice(-4)).join('-');
  return aa < bb ? -1 : (aa > bb ? 1 : 0);
});

// for (let x of gloresult ) console.log ( x.Date );
for (let x of gloresult ) console.log ( JSON.stringify( x ) );
.as-console-wrapper { max-height: 100% !important; top: auto; }