如何在 JavaScript 中对字符串进行排序?

javascriptweb developmentfront end technology

排序字符串是按字典或字母顺序排列字符串。在使用 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]) {
         // 交换索引 a 和索引 b 处的字符串
      }
   }
}

在上面的语法中,我们使用了两个嵌套的 for 循环并遍历字符串数组。此外,我们正在比较两个字符串值,并在此基础上交换字符串。

算法

步骤 1 - 创建字符串数组。

步骤 2 - 使用 for 循环并从第 0 个索引开始迭代字符串数组。

步骤 3 - 在 for 循环中,使用另一个 for 循环,并从 a+1 个索引开始迭代,此时 a 是第一个 for 循环的迭代指针。

步骤 4 - 现在,比较第 a 个和第 b 个索引处的字符串。

步骤 5 - 如果字符串在第 a 个索引处的字母顺序如果第 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() 方法,因为它始终按字母顺序对字符串进行排序。在第二种方法中,我们实现了冒泡排序算法来对字符串进行排序,但我们可以对其进行优化,使其更省时。此外,我们还可以使用其他算法(如合并排序)来使我们的排序算法更省时省空间。


相关文章