我需要对一个字符串数组进行排序,但我需要它以便 null 总是最后。例如,数组:
var arr = [a, b, null, d, null]
当升序排序[a, b, d, null, null]
时,我需要像[d, b, a, null, null]
.
这可能吗?我尝试了下面找到的解决方案,但这并不是我所需要的。
我需要对一个字符串数组进行排序,但我需要它以便 null 总是最后。例如,数组:
var arr = [a, b, null, d, null]
当升序排序[a, b, d, null, null]
时,我需要像[d, b, a, null, null]
.
这可能吗?我尝试了下面找到的解决方案,但这并不是我所需要的。
检查.sort()
并使用自定义排序进行操作。例子
function alphabetically(ascending) {
return function (a, b) {
// equal items sort equally
if (a === b) {
return 0;
}
// nulls sort after anything else
else if (a === null) {
return 1;
}
else if (b === null) {
return -1;
}
// otherwise, if we're ascending, lowest sorts first
else if (ascending) {
return a < b ? -1 : 1;
}
// if descending, highest sorts first
else {
return a < b ? 1 : -1;
}
};
}
var arr = [null, 'a', 'b', null, 'd'];
console.log(arr.sort(alphabetically(true)));
console.log(arr.sort(alphabetically(false)));
使用区null
分值的自定义比较函数:
arr.sort(function(a, b) {
return (a===null)-(b===null) || +(a>b)||-(a<b);
});
对于非空值的降序,只需交换a
和b
直接比较:
arr.sort(function(a, b) {
return (a===null)-(b===null) || -(a>b)||+(a<b);
});
上升
arr.sort((a, b) => (a != null ? a : Infinity) - (b != null ? b : Infinity))
降序
arr.sort((a, b) => (b != null ? b : -Infinity) - (a != null ? a : -Infinity))
(对于降序,如果数组中没有负值,我建议使用 0 而不是 -Infinity)
最简单的方法是先处理null
,然后根据所需顺序处理非空情况:
function sortnull(arr, ascending) {
// default to ascending
if (typeof(ascending) === "undefined")
ascending = true;
const multiplier = ascending ? 1 : -1;
const sorter = function(a, b) {
if (a === b) // identical? return 0
return 0;
else if (a === null) // a is null? last
return 1;
else if (b === null) // b is null? last
return -1;
else // compare, negate if descending
return a.localeCompare(b) * multiplier;
}
return arr.sort(sorter);
}
const arr = ["a", "b", null, "d", null];
console.log(sortnull(arr)); // ascending ["a", "b", "d", null, null]
console.log(sortnull(arr, true)); // ascending ["a", "b", "d", null, null]
console.log(sortnull(arr, false)); // descending ["d", "b", "a", null, null]
如果您需要对数字进行自然排序或由Collator
(包括速度增强和尊重语言环境)提供的任何选项,请尝试这种方法,基于 Paul Roub 的解决方案,稍微清理一下。我们几乎总是使用数字排序,因此默认...
如果您不是 Typescript 粉丝,只需去掉:type
规格或从代码片段中复制即可。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator
const naturalCollator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
const alphabeticCollator = new Intl.Collator(undefined, {});
function nullSort(descending: boolean = false, alphabetic: boolean = false) {
return function (a: any, b: any): number {
if (a === b) {
return 0;
}
if (a === null) {
return 1;
}
if (b === null) {
return -1;
}
let ret
if (alphabetic) {
ret = alphabeticCollator.compare(a, b)
} else {
ret = naturalCollator.compare(a, b)
}
if (descending) {
ret = -ret
}
return ret
};
}
像这样使用它。
// numeric, ascending (default)
myList.sort(nullSort());
// alphabetic, descending
myList.sort(nullSort(true, true));
您可以修改工厂方法以采用整理器,以获得更大的灵活性。
function nullSort(descending: boolean = false, collator: Collator = naturalCollator)
const naturalCollator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base'
});
const alphabeticCollator = new Intl.Collator(undefined, {});
function nullSort(descending = false, alphabetic = false) {
return function(a, b) {
if (a === b) {
return 0;
}
if (a === null) {
return 1;
}
if (b === null) {
return -1;
}
let ret
if (alphabetic) {
ret = alphabeticCollator.compare(a, b)
} else {
ret = naturalCollator.compare(a, b)
}
if (descending) {
ret = -ret
}
return ret
};
}
const items = [null, 10, 1, 100, null, 'hello', .1, null]
console.log(items.sort(nullSort()));