【问题标题】:Sorting JSON array by date value stopping after one iteration [duplicate]一次迭代后按日期值对JSON数组进行排序[重复]
【发布时间】:2019-05-25 23:25:04
【问题描述】:

所以我正在尝试按日期键对 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 页面上的按钮时我正在调用该函数。

【问题讨论】:

    标签: javascript arrays json date object


    【解决方案1】:

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

    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; }

    【讨论】:

    • 啊,我明白了,完美,谢谢
    • 没问题@chris,总是乐于提供帮助。
    • "Date 构造函数期望..." 不,它没有。除 ECMA-262 定义的 3 之外的任何格式的解析都取决于实现。对 m/d/y 的支持是一项遗留功能,并且在任何地方都没有标准化。您可以将值重新格式化为 ISO 8601 日期字符串,并让它们按词法排序,而不是创建日期。或者将它们的值组合为数字(yyyymmdd)并按数字排序。 ;-)
    【解决方案2】:

    这有点棘手,但它的作品!

    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; }

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 2013-04-23
      • 2018-02-09
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      相关资源
      最近更新 更多