怎么样用JavaScript对数字数组进行排序 超、凢脫俗 2024-04-01 16:06 14阅读 0赞 JavaScript的Array.sort()方法被用来就地[数组][Link 1]元素进行排序,并返回排序后的数组。此函数以字符串格式对元素进行排序。它对字符串数组有效,但对数字无效。例如:如果数字按字符串排序。例: * * * 输入:[12,25,31,23,75,81,100]错误的输出:[100、12、23、25、31、75、81]正确的输出:[12、23、25、31、75、81、100] 示例:本示例以[字符串][Link 2]格式对数组元素进行排序。 * * * * * * * * * * * * * * * * * * * <script>// Declare and initialize original array var marks = [12, 25, 31, 23, 75, 81, 100];// Print Before Sortring Array document.write("Original Array</br>"); document.write(marks);document.write("</br>");// Call sort fucntion marks.sort();document.write("</br>After Sorting in Ascending Order</br>");// Print Srted Numeric Array document.write(marks); </script> 输出: * * * * * 原始阵列12,25,31,23,75,81,100升序排序后100,12,23,25,31,75,81 那么,如何对数字数组元素进行排序?有两种方法可以对数字数组进行升序排序。 * 使用比较功能 * 通过创建循环 使用比较功能:我们可以创建一个比较功能,该功能返回负值,零值或正值。句法:[联联周边游][Link 3] * 函数(a,b){返回a-b} * 负值(a> b)=> a将放置在b之前 * 零值(a == b)=>不变 * 正值(a <b)=> a将位于b之后 示例:本示例使用compare函数对数组元素进行升序排序。 * * * * * * * * * * * * * * * * * * * <script>// Declare and initialize an Array var marks = [12, 25, 31, 23, 75, 81, 100];// Print Before sortring array document.write("Original Array</br>"); document.write(marks);document.write("</br>");// Sort elements using compare method marks.sort(function(a, b){return a - b});document.write("</br>After sorting in Ascending order</br>");// Print sorted Numeric array document.write(marks); </script> 输出: * * * * * 原始阵列12,25,31,23,75,81,100升序排序后12,23,25,31,75,81,100 现在,我们要以降序对数组进行排序,而不需要更改比较函数。句法: * 函数(a,b){返回b-a} * 负值(b <a)=> a将位于b之后 * 零值(a == b)=>不变 * 正值(b> a)=> a将位于b之前 示例:本示例使用compare函数对数组元素进行降序排序。 * * * * * * * * * * * * * * * * * * * <script>// Declare and initialize an Array var marks = [12, 25, 31, 23, 75, 81, 100];// Print Before sortring array document.write("Original Array</br>"); document.write(marks);document.write("</br>");// Sort elements using compare method marks.sort(function(a, b){return b - a});document.write("</br>After sorting in Ascending order</br>");// Print sorted Numeric array document.write(marks); </script> 输出: * * * * * 原始阵列12,25,31,23,75,81,100升序排序后100,81,75,31,25,23,12 创建循环:我们还可以使用循环对数组元素进行排序。在这里,我们将使用冒泡排序(简单排序技术)对数组元素进行升序排序。例: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * <script>// Sorting function function Numeric_sort(ar) { var i = 0, j; while (i < ar.length) { j = i + 1; while (j < ar.length) { if (ar[j] < ar[i]) { var temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; } j++; } i++; } }// Original Array var arr = [1, 15, 10, 45, 27, 100];// Print Before sortring array document.write("Original Array</br>"); document.write(arr);document.write("</br>");// Function call Numeric_sort(arr);document.write("</br>Sorted Array</br>");// Print sorted Numeric array document.write(arr); </script> 输出: * * * * * 原始阵列1,15,10,45,27,100排序数组1,10,15,27,45,100 来源:[https://www.weidianyuedu.com][https_www.weidianyuedu.com] [Link 1]: https://so.csdn.net/so/search?q=%E6%95%B0%E7%BB%84&spm=1001.2101.3001.7020 [Link 2]: https://so.csdn.net/so/search?q=%E5%AD%97%E7%AC%A6%E4%B8%B2&spm=1001.2101.3001.7020 [Link 3]: https://t.cn/AiB8yGPP [https_www.weidianyuedu.com]: https://www.weidianyuedu.com
还没有评论,来说两句吧...