[英].sort not working in IE (IE 9)
Ive worked for a few days on this one. 我已经在这个工作了几天。 I have a script that is dynamically creating rows in a table.
我有一个动态创建表中的行的脚本。 The final output looks like this:
最终输出如下所示:
<table id="event-list">
<tr id="event0" value="04/04/2000">
<td>My Third Event</td>
<td>Apr 4</td>
<td>Omaha, NE</td>
<td></td>
</tr>
<tr id="event4" value="04/24/2000">
<td>USA Triathlon National Championships Collegiate Club</td>
<td>Apr 24</td><td>Clemson, SC</td>
<td></td>
</tr>
<tr id="event2" value="07/17/2000">
<td>My Second Event</td><td>Jul 17</td>
<td>Omaha, NE</td>
<td></td>
</tr>
<tr id="event3" value="08/17/2000">
<td>My Second Event</td>
<td>Aug 17</td>
<td>Ames, IA</td>
<td></td>
</tr>
<tr id="event1" value="10/26/2000">
<td>ZTA 5K For Breast Cancer Awareness</td>
<td>Oct 26</td>
<td>Omaha, NE</td>
<td></td>
</tr>
</table>
Im using the below script to sort each row by date: 我使用以下脚本按日期对每一行进行排序:
$('#event-list tr').sort(function(a,b) {
return new Date($(a).attr('value')) < new Date($(b).attr('value'));
}).each(function(){
$('#event-list').prepend(this);
});
It works fine in Chrome and Firefox, but in IE - the list is just reversing in the opposite order. 它在Chrome和Firefox中运行良好,但在IE中 - 列表只是以相反的顺序反转。
Any thoughts on a reason or work around? 有关理由或解决方法的任何想法? Thanks in advance.
提前致谢。
Your sort function is incorrect. 您的排序功能不正确。 A sort function can't just return a boolean telling whether one element is greater than the other.
sort函数不能只返回一个布尔值,告诉一个元素是否大于另一个元素。 It has to handle all three cases: less than, greater than, and equal.
它必须处理所有三种情况:小于,大于和等于。
It's easy to change your sort function to accomplish this. 更改排序功能很容易实现此目的。 Instead of comparing the two values with the
<
operator, simply subtract them: 不是将这两个值与
<
运算符进行比较,而是简单地减去它们:
return new Date($(a).attr('value')) - new Date($(b).attr('value'));
If that gives you the opposite order from what you want, reverse it: 如果这给你的顺序与你想要的相反,那么反过来:
return new Date($(b).attr('value')) - new Date($(a).attr('value'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.