如何在 JavaScript 中排序字符串?
字符串排序是指按照字典顺序或字母顺序排列字符串。在使用 JavaScript 开发应用程序时,通常需要对字符串数组进行排序。在本教程中,我们将学习如何排序JavaScript 中的字符串。
例如,如果您从 API 获取了一些数据并希望按排序顺序显示这些数据,则字符串排序在这里非常有用。
在这里,我们将学习如何使用内置方法和各种简单方法对字符串进行排序。
使用 sort() 方法排序字符串
在JavaScript中,sort() 是我们可以与数组一起使用的内置方法。通常,在其他编程语言中,sort() 方法默认按数字值排序。但是,JavaScript 会将数字转换为字符串并按字母顺序对其进行排序。
因此,我们可以使用 JavaScript 的sort() 方法而不使用比较器函数来对字符串数组进行排序。
语法
用户可以按照以下语法使用 JavaScript 的 sort() 方法对字符串进行排序。
Strings.sort();
在上面的语法中,我们使用字符串数组作为引用和 sort() 方法。
示例 1
在这个例子中,我们定义了字符串数组并用一些字符串值对其进行了初始化。之后,我们以数组作为引用,并对数组执行了 sort() 方法。用户可以观察到输出结果:数组中的所有字符串都按字母顺序排序。
<html>
<body>
<h2>Using the <i>sort() method</i> to sort an array of strings in JavaScript.</h2>
<div id = "output"> </div>
<script>
let output = document.getElementById('output');
let strings = ["Hi", "JavaScript", "TypeScript", "C", "CPP", "Python", "Java", "HTML", "CSS"];
output.innerHTML += "The original string array is " + strings + "<br/>";
strings.sort();
output.innerHTML += "The sorted string array is " + strings + "<br/>";
</script>
</body>
</html>
使用 for 循环排序字符串(冒泡排序算法)
排序字符串的简单方法是使用for 循环。我们可以使用两个嵌套 for 循环来比较每个字符串与所有其他字符串,并按字母顺序对其进行排序。我们也可以称之为冒泡排序算法。
语法
用户可以按照以下语法使用冒泡排序算法按字母顺序对字符串进行排序。
for (let a = 0; a < strings.length; a++) {
for (let b = a + 1; b < strings.length; b++) {
if (strings[a] > strings[b]) {
// swap strings at index a and index b
}
}
}
在上面的语法中,我们使用了两个嵌套的 for 循环并遍历字符串数组。此外,我们正在比较两个字符串值,并根据此结果交换字符串。
算法
步骤 1 − 创建字符串数组。
步骤 2 − 使用 for 循环并从第 0 个索引开始遍历字符串数组。
步骤 3 − 在 for 循环中,使用另一个 for 循环,当 a 是第一个 for 循环的迭代指针时,从 a+1 个索引开始迭代。
步骤 4 − 现在,比较第 a 个和第 b 个索引处的字符串。
步骤 5 − 如果第 a 个索引处的字符串的字母顺序大于第 b 个索引处的字符串,则交换这两个字符串。
步骤 6 − 完成两个 for 循环的所有迭代以按排序顺序获取所有字符串。
示例 2(考虑字符串字符的大小写)
在下面的示例中,我们实现了冒泡排序算法来对字符串数组进行排序。下面的输出向我们展示了冒泡排序算法如何将所有带有大写字母的字符串排在大写字母之前的顺序,因为在大写字母的字符串比较中,大写字母的优先级高于小写字母。
<html>
<body>
<h2>Using the <i> bubble sort algorithm </i> to sort an array of strings in JavaScript.</h2>
<div id = "output"> </div>
<script>
let output = document.getElementById('output');
let strings = ["car", "Bike", "truck", "cycle", "Tempo", "cart", "abcd", "string"];
output.innerHTML += "The original string array is " + strings + "<br/>";
for (let a = 0; a < strings.length; a++) {
for (let b = a + 1; b < strings.length; b++) {
if (strings[a] > strings[b]) {
let tempString = strings[a];
strings[a] = strings[b];
strings[b] = tempString;
}
}
}
output.innerHTML += "The sorted string array is " + strings + "<br/>";
</script>
</body>
</html>
示例 3(忽略字符串字符的大小写)
在这个示例中,我们实现了冒泡排序算法来对字符串进行排序,但是我们正在比较小写字符串。在上面的例子中,我们根据字母顺序对字符串进行了排序,并优先考虑大写字符串。但是在这里,我们忽略了字符串字符的大小写并进行字符串比较。
<html>
<body>
<h2>Using the <i> bubble sort algorithm </i> to sort an array of strings in JavaScript.</h2>
<div id = "output"> </div>
<button onclick = "sortStrings()"> Sort Strings </button>
<script>
let output = document.getElementById('output');
let strings = ["ab", "Bc", "AB", "AC", "cd", "ds", "ds", "erere", "DS"];
output.innerHTML += "The original strings are " + strings + "<br/>";
function sortStrings() {
function swap(index1, index2) {
let tempString = strings[index1];
strings[index1] = strings[index2];
strings[index2] = tempString;
}
for (let a = 0; a < strings.length; a++) {
for (let b = a + 1; b < strings.length; b++) {
if (strings[a].toLowerCase() > strings[b].toLowerCase()) {
swap(a, b)
}
}
}
output.innerHTML += "The sorted strings are " + strings + "<br/>";
}
</script>
</body>
</html>
在本教程中,我们学习了如何对多个字符串进行排序。在第一种方法中,我们使用了 sort() 方法,因为它始终按字母顺序对字符串进行排序。在第二种方法中,我们实现了冒泡排序算法来对字符串进行排序,但是我们可以对其进行优化以提高其时间效率。此外,我们还可以使用其他算法(如归并排序)来提高排序算法的时间和空间效率。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP