JavaScript |排序()方法
array.sort()是 JavaScript 中的一个内置方法,用于对数组进行排序。数组可以是任何类型,即字符串、数字、字符等。
句法:
array.sort()
这里数组是要排序的一组值。
参数:它不接受任何参数。
返回值:它不返回任何内容。
例子:
Input: var arr = ["Manish", "Rishabh", "Nitika", "Harshita"];
Output: Harshita, Manish, Nitika, Rishabh
Input: var arr = [1, 4, 3, 2];
Output: 1, 2, 3, 4
代码 #1:对字符串数组进行排序:
Click on the Sort button to sort the array
输出:
在点击“排序”按钮之前——
点击“排序”按钮后——
代码 #2:对整数数组进行排序:
Click on the Sort button to sort the array
输出:
在点击“排序”按钮之前——
点击“排序”按钮后——