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

 所属分类:web前端开发

 浏览:38次-  评论: 0次-  更新时间:2023-09-19
描述:更多教程资料进入php教程获得。 排序字符串是将字符串按&#2338...
更多教程资料进入php教程获得。

如何在 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+1 个索引,此时 a 是第一个 for 循环的迭代指针。 p>

第 4 步 - 现在,比较 ath 和 bth 索引处的字符串。

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

 标签:
积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!