在 JavaScript 中对 NULL 值排在最后的数组进行排序
Sort an Array with NULL values coming last in JavaScript
要对 NULL 值排在最后的数组进行排序:
- 调用
sort()
数组上的方法,向其传递一个函数。 - 该函数定义排序顺序。
null
最后对值进行排序。
const arr = ['c', null, 'z', null, 'b', null, 'a']; // ✅ Sort Ascending (low to high) const sortedAsc = arr.sort((a, b) => { if (a === null) { return 1; } if (b === null) { return -1; } if (a === b) { return 0; } return a < b ? -1 : 1; }); console.log(sortedAsc); // 👉️ ['a', 'b', 'c', 'z', null, null, null]
null
如果您需要按降序对包含值的数组进行排序,请改用以下代码示例。
const arr = ['c', null, 'z', null, 'b', null, 'a']; // ✅ Sort Descending (high to low) const sortedDesc = arr.sort((a, b) => { if (a === null) { return 1; } if (b === null) { return -1; } if (a === b) { return 0; } return a < b ? 1 : -1; }); console.log(sortedDesc); // 👉️ ['z', 'c', 'b', 'a', null, null, null]
我们使用Array.sort()方法对数组进行排序,其中的null
值排在最后。
sort()
方法对数组的元素进行就地排序并返回排序后的数组。换句话说,它改变了原始数组。const arr = ['c', null, 'z', null, 'b', null, 'a']; const sortedAsc = arr.sort((a, b) => { if (a === null) { return 1; } if (b === null) { return -1; } if (a === b) { return 0; } return a < b ? -1 : 1; }); console.log(sortedAsc); // 👉️ ['a', 'b', 'c', 'z', null, null, null] // 👇️ (Original array also changed) console.log(arr); // 👉️ ['a', 'b', 'c', 'z', null, null, null]
对 NULL 值排在最后的数组进行排序,没有突变
如果您想对数组进行排序而不改变它,请在调用该方法之前使用扩展语法 (…) 创建一个浅表副本sort()
。
const arr = ['c', null, 'z', null, 'b', null, 'a']; // 👇️ create shallow copy before calling sort() const sortedAsc = [...arr].sort((a, b) => { if (a === null) { return 1; } if (b === null) { return -1; } if (a === b) { return 0; } return a < b ? -1 : 1; }); console.log(sortedAsc); // 👉️ ['a', 'b', 'c', 'z', null, null, null] console.log(arr); // 👉️ ['c', null, 'z', null, 'b', null, 'a']
在调用该方法之前,我们使用
扩展语法 (…)将数组的值解压缩到一个新数组中sort
。
我们传递给该方法的参数sort
是一个定义排序顺序的函数。
const arr = ['c', null, 'z', null, 'b', null, 'a']; const sortedAsc = arr.sort((a, b) => { if (a === null) { return 1; } if (b === null) { return -1; } if (a === b) { return 0; } return a < b ? -1 : 1; }); console.log(sortedAsc); // 👉️ ['a', 'b', 'c', 'z', null, null, null]
该sort()
方法通过以下方式确定数组中元素的顺序:
-
如果比较函数的返回值大于
0
,则b
先排序a
。 -
If the return value of the compare function is less than
0
, then sorta
beforeb
. -
If the return value of the compare function is equal to
0
, keep the original
order ofa
andb
.
In our example, this translates to:
-
If
a
has a value ofnull
, we return1
and therefore sortb
beforea
. -
If
b
has a value ofnull
, we return-1
and sorta
beforeb
. -
If
a
andb
are equal, we return0
to keep the original order ofa
and
b
. -
Otherwise, we check if
a
is less thanb
and return-1
, else we return
1
.
# Sort an Array with NULL values coming last in descending order
All you have to do to change the order to descending is change the last line in
the callback function.
const arr = ['c', null, 'z', null, 'b', null, 'a']; const sortedDesc = arr.sort((a, b) => { if (a === null) { return 1; } if (b === null) { return -1; } if (a === b) { return 0; } return a < b ? 1 : -1; }); console.log(sortedDesc); // 👉️ ['z', 'c', 'b', 'a', null, null, null]
# Additional Resources
You can learn more about the related topics by checking out the following
tutorials:
- How to sort a Set in JavaScript
- Sort an Array of Objects by Boolean property in JavaScript
- Sort an Array of Objects by Date property in JavaScript
- Sort an Array of Strings in Descending order in JavaScript
- Sort an Array without Mutation using JavaScript
- Sort an Array of strings ignoring the Case in JavaScript
- How to sort a Map in JavaScript
- Sort the Keys of an Object in JavaScript