在 JavaScript 中按降序对字符串数组进行排序
Sort an Array of Strings in Descending order in JavaScript
要按降序对字符串数组进行排序:
- 调用
sort()
数组上的方法。 reverse()
在结果上调用方法。- 返回的数组将按降序排列其元素。
// ✅ Sort in Descending order const strArr1 = ['a', 'c', 'z', 'f']; const descArr = strArr1.sort().reverse(); console.log(descArr); // 👉️ ['z', 'f', 'c', 'a'] // ✅ Sort in Descending order (Alternative) const strArr2 = ['a', 'c', 'z', 'f']; const descArr2 = strArr2.sort((a, b) => (a > b ? -1 : 1)); console.log(descArr2); // 👉️ ['z', 'f', 'c', 'a'] // ✅ Sort in ascending order const strArr3 = ['z', 'c', 'a', 'f']; const ascArr = strArr3.sort(); console.log(ascArr); // 👉️ ['a', 'c', 'f', 'z']
我们使用Array.sort()方法按降序对字符串数组进行排序。
sort()
方法对数组的元素进行就地排序并返回排序后的数组。换句话说,它改变了原始数组。// ✅ Sort in Descending order const strArr1 = ['a', 'c', 'z', 'f']; const descArr = strArr1.sort().reverse(); console.log(descArr); // 👉️ ['z', 'f', 'c', 'a'] console.log(strArr1); // 👉️ ['z', 'f', 'c', 'a']
不改变地按降序对字符串数组进行排序
如果您想对数组进行排序而不改变它,请在调用该方法之前使用扩展语法 (…) 创建一个浅表副本sort()
。
// ✅ Sort in Descending order const strArr1 = ['a', 'c', 'z', 'f']; const descArr = [...strArr1].sort().reverse(); console.log(descArr); // 👉️ ['z', 'f', 'c', 'a'] console.log(strArr1); // 👉️ ['a', 'c', 'z', 'f'] // ✅ Sort in Descending order (Alternative) const strArr2 = ['a', 'c', 'z', 'f']; const descArr2 = [...strArr2].sort((a, b) => (a > b ? -1 : 1)); console.log(descArr2); // 👉️ ['z', 'f', 'c', 'a']
在调用该方法之前,我们使用
扩展语法 (…)将数组的值解压缩到一个新数组中sort
。
sort()
and方法可能是
代码示例中reverse()
最容易阅读的方法。2
// ✅ Sort in Descending order const strArr1 = ['a', 'c', 'z', 'f']; const descArr = strArr1.sort().reverse(); console.log(descArr); // 👉️ ['z', 'f', 'c', 'a']
在不带任何参数调用时,该sort
方法将数组元素转换为字符串(如有必要)并根据它们的 UTF-16 代码单元值对它们进行排序。
Array.reverse方法就地反转数组并返回结果。
// 👇️ ['c', 'b',' 'a'] console.log(['a', 'b', 'c'].reverse());
The parameter we passed to the sort
method in the second example is a function
that defines the sort order.
const strArr2 = ['a', 'c', 'z', 'f']; const descArr2 = [...strArr2].sort((a, b) => (a > b ? -1 : 1)); console.log(descArr2); // 👉️ ['z', 'f', 'c', 'a']
If the function parameter is not provided to the sort
method, the array
elements get converted to strings and sorted according to their UTF-16 code unit
values.
This is not what we want when we need to sort an array of strings in descending
order, however, it’s exactly what we want when sorting string arrays in
ascending order.
If the return value of the compare function is greater than 0
, then sort b
before a
.
If the return value of the compare function is less than 0
, then sort a
before b
.
If the return value of the compare function is equal to 0
, keep the original
order of a
and b
.
# 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 with NULL values coming last 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