Javascript 如何对包含空单元格的日期列进行排序
我正在使用在上找到的sorttable.js脚本 我遇到的一个问题是,对于混合了空单元格和日期的日期列,排序不起作用。我的日期是“MM/DD/YYYY”格式 据我所见,排序发生在脚本中:Javascript 如何对包含空单元格的日期列进行排序,javascript,sorttable.js,Javascript,Sorttable.js,我正在使用在上找到的sorttable.js脚本 我遇到的一个问题是,对于混合了空单元格和日期的日期列,排序不起作用。我的日期是“MM/DD/YYYY”格式 据我所见,排序发生在脚本中: sort_mmdd: function(a,b) { mtch = a[0].match(sorttable.DATE_RE); y = mtch[3]; d = mtch[2]; m = mtch[1]; if (m.length == 1) m = '0'+m; if (d.
sort_mmdd: function(a,b) {
mtch = a[0].match(sorttable.DATE_RE);
y = mtch[3]; d = mtch[2]; m = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt1 = y+m+d;
mtch = b[0].match(sorttable.DATE_RE);
y = mtch[3]; d = mtch[2]; m = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt2 = y+m+d;
if (dt1==dt2) return 0;
if (dt1<dt2) return -1;
return 1;
}
排序:函数(a,b){
mtch=a[0]。匹配(排序表日期\u RE);
y=mtch[3];d=mtch[2];m=mtch[1];
如果(m.length==1)m='0'+m;
如果(d.length==1)d='0'+d;
dt1=y+m+d;
mtch=b[0]。匹配(排序表日期);
y=mtch[3];d=mtch[2];m=mtch[1];
如果(m.length==1)m='0'+m;
如果(d.length==1)d='0'+d;
dt2=y+m+d;
if(dt1==dt2)返回0;
如果(dt1很好,那么当没有匹配项时,您可以返回一个数组,该匹配项要么在遥远的将来,要么在遥远的过去
mtch = a[0].match(sorttable.DATE_RE)|| ['nomatch','01','01','9000'];
我想出了如何让日期与空白单元格一起工作(见下文) 最终测试“mtch”,然后再使用它
sort_mmdd: function(a,b) {
mtch = a[0].match(sorttable.DATE_RE);
if ((mtch == null) || (mtch == undefined)) {
y = 0; d = 0; m = 0;
}
else {
y = mtch[3]; d = mtch[2]; m = mtch[1];
}
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt1 = y+m+d;
mtch = b[0].match(sorttable.DATE_RE);
if ((mtch == null) || (mtch == undefined)) {
y = 0; d = 0; m = 0;
}
else {
y = mtch[3]; d = mtch[2]; m = mtch[1];
}
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt2 = y+m+d;
if (dt1==dt2) return 0;
if (dt1<dt2) return -1;
return 1;
}
排序:函数(a,b){
mtch=a[0]。匹配(排序表日期\u RE);
if((mtch==null)| |(mtch==未定义)){
y=0;d=0;m=0;
}
否则{
y=mtch[3];d=mtch[2];m=mtch[1];
}
如果(m.length==1)m='0'+m;
如果(d.length==1)d='0'+d;
dt1=y+m+d;
mtch=b[0]。匹配(排序表日期);
if((mtch==null)| |(mtch==未定义)){
y=0;d=0;m=0;
}
否则{
y=mtch[3];d=mtch[2];m=mtch[1];
}
如果(m.length==1)m='0'+m;
如果(d.length==1)d='0'+d;
dt2=y+m+d;
if(dt1==dt2)返回0;
if(dt1+1)-谢谢。这是一个正确的想法(也很有帮助),但效果不太好。我认为可能是“a[0]。匹配…”正在返回未定义的…无论哪种方式,我找到了一种方法,并将在片刻后发布我的答案。请随意评论可能会有所改进的内容。只是查看了您的个人资料,发现您是一名缅因州同胞-毕竟这是一个小世界!我看到您的名字是“kennebec”这对我来说似乎很可疑……我住在布伦瑞克;还没去过雷德菲尔德。。。