简体   繁体   English

如何在javascript中的列表中对日期进行排序?

[英]How to sort date in column table in javascript?

Since I am a beginner, I don't know how to start sorting the date column. 由于我是初学者,所以我不知道如何开始对日期列进行排序。 I have another column which is total price in my table. 我在表中还有另一列是总价。 I managed to sort the price column since it only involves number only. 我设法对价格列进行排序,因为它仅涉及数字。

The codes I provided below is how I successfully sort price column. 我在下面提供的代码是如何成功对价格列进行排序的。

在此处输入图片说明

//function to sort number
  function sortLowest() {
        var table, rows, switching, i, x, y, shouldSwitch;
        table = document.getElementById("example");
        switching = true;
        while (switching) {
            switching = false;
            rows = table.rows;
            for (i = 1; i < (rows.length - 1); i++) {
                shouldSwitch = false;
                x = rows[i].getElementsByTagName("TD")[6];
                y = rows[i + 1].getElementsByTagName("TD")[6];
                if (parseInt(x.innerHTML.toLowerCase()) > parseInt(y.innerHTML.toLowerCase())) {
                    shouldSwitch = true;
                    break;
                }
            }
            if (shouldSwitch) {
                rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
                switching = true;
            }
        }
    }

Below are the html code 以下是html代码

  <p id="rating">Filter by Price:
  <select id="myList" class='form-control' style="width: 100%">
  <option id="stylesheet1" value="low">Lowest-Highest</option>
  <option id="stylesheet2" value="high">Highest-Lowest</option>
  </select></p>

<script>
  document.getElementById("myList").onchange = function() {
        var sheet = document.getElementById("myList").value;
        if (sheet == "high") {
            sortHighest();
        } else {
            sortLowest();
        }
        return false
    };
</script>

It would definitely be easier with a sort method but I would assume that you need to use a similar logic as the one above. 使用sort方法肯定会更容易,但是我认为您需要使用与上面类似的逻辑。 You're also dealing with dates so you need to convert your string/text to dates before sorting; 您还需要处理日期,因此需要在排序之前将字符串/文本转换为日期; meaning using Date.now() or new Date(). 意思是使用Date.now()或new Date()。

I guess you do get the rows with rows = table.rows; 我猜你确实得到了rows = table.rows; and that getElementsByTagName("TD")[7] gives you the date column (I'm using 7 as an example since you wrote 6 as the price in your description so please use whatever column index is correct to get the dates for your problem). 并且getElementsByTagName("TD")[7]为您提供日期列(我以7为例,因为您在描述中将价格写为6,所以请使用正确的任何列索引来获取问题的日期)。 If these assumptions are correct, you can re-order by doing something like this and using a similar logic as above for dates: 如果这些假设是正确的,则可以通过执行以下操作并对日期使用类似的逻辑来重新排序:

        while (switching) {
            switching = false;
            rows = table.rows;
            for (i = 1; i < (rows.length - 1); i++) {
                shouldSwitch = false;
                x = new Date(rows[i].getElementsByTagName("TD")[7].innerHTML.split('/').reverse().join(', '));
                y = new Date(rows[i + 1].getElementsByTagName("TD")[7].innerHTML.split('/').reverse().join(', '));

                if (x > y) {
                    shouldSwitch = true;
                    break;
                }
            }
            if (shouldSwitch) {
                rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
                switching = true;
            }
        }

nb: The reason why I re-ordered '16/09/2019' to be '2019, 09, 16' with .split('/').reverse().join(', ') is because something like new Date('16/09/2019') does not work as opposed to new Date('2019, 09, 16') which does. nb:之所以用.split('/').reverse().join(', ')将'16 / 09/2019'重新排序为' .split('/').reverse().join(', ')原因是因为类似new Date('16/09/2019')new Date('2019, 09, 16') new Date('16/09/2019')无效。

Please let me know if this solution solves your issue or if you have any question. 如果此解决方案可以解决您的问题或有任何疑问,请告诉我。

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

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