简体   繁体   English

Array.sort无法正常工作

[英]Array.sort not working correctly

I need the sort function to sort the dates from the earliest date to the latest date. 我需要sort函数对最早的日期到最晚的日期进行排序。 What can I do to fix this in my tasks table? 如何在任务表中解决此问题?

var tasks = new Array();
var index = 0;

function addTask() {
    var temptask = document.getElementById("taskinfo").value;
    var td = document.getElementById("taskdate").value;
    var tempdate = new Date(td);

    //add array and populate from tempdate and temptask
    //generate html table from 2d javascript array
    tasks[index] = {
        Date: tempdate,
        Task: temptask,
    };  

    index++

    tasks.sort(function(a,b){return new Date(b.Date).getTime() - new Date(a.Date).getTime()});

    var tablecode = "<table class = 'tasktable'>" +
        "<tr>"+
        "<th>Date</th>"+
        "<th>Task</th>"+
        "</tr>";
    for (var i = 0; i < tasks.length; i++) {
        tablecode = tablecode + "<tr>" +
            "<td>" + tasks[i]["Date"].toDateString() + " </td>" +
            "<td>" + tasks[i]["Task"] + " </td>" +
            "</tr>";
    }

    tablecode = tablecode + "</table>";

    document.getElementById("bottomright").innerHTML = tablecode;

    return false;
}

I have tried many different syntax variations and can not get the sort function to sort in descending order 我尝试了许多不同的语法变体,但无法获得sort功能以降序排序

You're subtracting a.Date from b.Date , exactly the reverse of what you want. 您要从b.Date减去a.Date ,恰好与您想要的相反。

Flip those around (and remove the unnecessary new Date() wrappers, although they're not actually breaking anything) and you'll get the correct sort: 翻转它们(并删除不必要的new Date()包装器,尽管它们实际上并没有破坏任何东西),您将获得正确的排序:

 var tasks = [], index = 0; function addTask() { var temptask = document.getElementById("taskinfo").value; var td = document.getElementById("taskdate").value; var tempdate = new Date(td); tasks[index] = { Date: tempdate, Task: temptask, }; index++ tasks.sort(function(a, b) { return a.Date.getTime() - b.Date.getTime() }); var tablecode = "<table class='tasktable'>" + "<tr>" + "<th>Date</th>" + "<th>Task</th>" + "</tr>"; for (var i = 0; i < tasks.length; i++) { tablecode += "<tr>" + "<td>" + tasks[i]["Date"].toDateString() + " </td>" + "<td>" + tasks[i]["Task"] + " </td>" + "</tr>"; } tablecode += "</table>"; document.getElementById("bottomright").innerHTML = tablecode; return false; } document.getElementById('add').addEventListener('click', addTask); 
 <p>Task: <input type="text" id="taskinfo" /></p> <p>Date: <input type="date" id="taskdate" /></p> <button id="add">add</button> <div id="bottomright"></div> 

Since the date is represented as 由于日期表示为

the number of milliseconds since 1 January, 1970 UTC 自1970年1月1日UTC以来的毫秒数
( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date ) https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date

the sorting order you are looking for is ascending not descending. 您要查找的排序顺序是升序而不是降序。 Also, as @birdspider already commented, there is no use of creating new Date objects and invoking the getTime() method. 另外,正如@birdspider所评论的那样,没有使用创建新的Date对象和调用getTime()方法的用法。 They are comparable as they are. 它们具有可比性。

To summarize the above points, try using the following sorting function: 总结以上几点,请尝试使用以下排序功能:

function sortDatesAsc(tempdateA, tempdateB) {
    return tempdateA - tempdateB < 0 ? -1 : (tempdateA > tempdateB ? 1 : 0);
}

tasks.sort(sortDatesAsc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM