如何对具有null值的数组进行排序
How to sort an array with null values
有很多问题与该主题有关,但我找不到适合我的案例的正确解决方案。
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]
我做错了什么?
function getSort (ascending) {
// if ascending, `null` will be pushed towards the end of the array by returning 1
var nullPosition = ascending ? 1 : -1
return function (a, b) {
// if a is null, push it towards whichever end null elements should end up
if (a == null) return nullPosition
// Note: at this point, a is non-null (previous if statement handled that case).
//
// If b is null, it must therefore be placed closer to whichever end the null
// elements should end up on. If ascending, null elements are pulled towards
// the right end of the array. If descending, null elements are pulled towards
// the left.
//
// Therefore, we return -nullPosition. If ascending, this is -1, meaning a comes
// before b; if descending, this is 1, meaning a comes after b. This is
// clearly the correct behavior, since ascending will push b, which is null,
// towards the end of the array (with -1) and descending will push b towards
// the beginning of the array.
if (b == null) return -nullPosition
// OTHERWISE, both elements are non-null, so sort normally.
// if a < b AND
// if ascending, a comes first, so return -1 == -nullPosition
// if descending, a comes after, so return -nullPosition == -(-1) == 1
if (a < b) return -nullPosition
// return the opposite of the previous condition
if (a > b) return nullPosition
// return 0 if both elements are equal
return 0
}
}
function write (arr) { arr.forEach(function (d) { document.write(d + "<br>")})}
var toSort = ['a', 'b', null, 'd', null]
var sortA = getSort(true)
var sortD = getSort(false)
document.write("<br>ASCENDING<br>")
write(toSort.sort(sortA))
document.write("<br>DESCENDING<br>")
write(toSort.sort(sortD))
您可以使用两步方法,先检查null
值,然后按字符串排序。
要更改排序顺序,可以交换参数或使用其中一个函数的否定结果。
var data = ['a', 'b', null, 'd', null];
// ascending
data.sort(function (a, b) {
return (a === null) - (b === null) || ('' + a).localeCompare(b);
});
console.log(data);
// descending
data.sort(function (a, b) {
return (b === null) - (a === null) || ('' + b).localeCompare(a);
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
下面的解决方案怎么样?
var arr = [null, 'e', 'a', 'b', null, 'd', null];
function sortBy(arr, ascending) {
return arr.sort((a, b) => {
if(!a) return ascending ? 1 : -1;
if(!b) return ascending ? -1 : 1;
if (ascending) return a > b ? 1 : -1;
return a > b ? -1 : 1;
})
}
const ascendingArr = sortBy(arr, true);
console.log(ascendingArr);
const decendingArr = sortBy(arr, false);
console.log(decendingArr);
相关文章:
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- Javascript中的多维数组排序索引问题
- 如何按日期对Javascript对象数组排序
- 数组排序后显示更改
- 当许多元素相等时,Javascript数组排序无法正常工作
- Javascript数组排序速度受字符串长度的影响
- 按唯一键将对象数组排序为数组
- 以 js 为单位的数组排序
- js 数组排序无法正常工作
- 数组排序.论点从何而来
- 不需要的数组排序
- 数组排序不正确/不可预测,使用 indexOf 时
- Javascript将数组排序到树中
- 随后的多维数组排序会产生意外的结果
- 数组排序基于纯javascript搜索文本匹配
- Javascript自定义数组按数组排序
- 使用字符串按属性错误对对象进行数组排序
- 多维数组排序
- JavaScript与PHP在数组排序中的对比
- Javascript-关联数组排序