有相当多的问题与这个主题有关,但我找不到适合我的情况的正确解决方案。
var arr = [a, b, null, d, null]我用下面的逻辑对这个数组进行排序
return function(a,b){
if(a === null){
return 1;
}
else if(b === null){
return -1;
}
else if(a === b){
return 0;
}
else if(ascending) {
return a < b ? -1 : 1;
}
else if(!ascending) {
return a < b ? 1 : -1;
}
};我得到了以下输出
Ascending : [a, b, d, null,null]
Descending : [d, b, a, null,null]
Expected : [null, null,d, b, a]我做错了什么?
https://stackoverflow.com/questions/33684937
复制相似问题