Javascript 按升序排序';t磨合功能

Javascript 按升序排序';t磨合功能,javascript,Javascript,这是我的演示: 我不知道为什么我的函数sortByPrice()不起作用。当我删除sortByPrice函数并只在里面留下代码时,它会工作,并在加载时自动显示价格上升 你在任何地方调用sortByPrice吗?tableOfCars是一个数组;在生成表之后对其进行排序将绝对不会对表产生任何影响,您需要在排序完成后重建表。那个么如何修复它呢?在返回后,我调用函数showTable(),然后按钮工作,但表出现了两次/非常好,谢谢:)但是我不理解while循环,while循环说,当表体节点的first

这是我的演示:


我不知道为什么我的函数sortByPrice()不起作用。当我删除sortByPrice函数并只在里面留下代码时,它会工作,并在加载时自动显示价格上升

你在任何地方调用
sortByPrice
吗?
tableOfCars
是一个数组;在生成表之后对其进行排序将绝对不会对表产生任何影响,您需要在排序完成后重建表。那个么如何修复它呢?在返回后,我调用函数showTable(),然后按钮工作,但表出现了两次/非常好,谢谢:)但是我不理解while循环,while循环说,当表体节点的firstChild节点不为null时,从DOM中删除第一个子节点。
function sortByPrice() {
    // sort the cars
    tableOfCars.sort(function (a, b) {
        return a.price - b.price;
    });
    // empty the table
    var tbody = document.getElementById("tabcars").getElementsByTagName("tbody")[0];
    while (tbody.firstChild) {
        tbody.removeChild(t.firstChild);
    }
    // rebuild the table
    showTable();
}
function sortByPrice() {
    // sort the cars
    tableOfCars.sort(function (a, b) {
        return a.price - b.price;
    });
    // empty the table
    var tbody = document.getElementById("tabcars").getElementsByTagName("tbody")[0];
    while (tbody.firstChild) {
        tbody.removeChild(t.firstChild);
    }
    // rebuild the table
    showTable();
}