Sort an array of numbers using JavaScript

The JavaScript Array.sort() method is used to sort array elements in-place and returns the sorted array. This function sorts the elements in string format. It works for arrays of strings, but not for numbers. For example: if numbers are sorted by string. example:-

输入:[12,25,31,23,75,81,100]
错误的输出:[100、12、23、25、31、75、81]
正确的输出:[12、23、25、31、75、81、100]

Example: This example sorts array elements in string format.

<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>

output:

原始阵列
12,25,31,23,75,81,100

升序排序后
100,12,23,25,31,75,81

So, how to sort numeric array elements?
There are two ways to sort an array of numbers in ascending order.

  • Use the compare function
  • by creating a loop

Using a comparison function: We can create a comparison function that returns a negative, zero or positive value.
syntax:

函数(a,b){返回a-b}
  • Negative values ​​(a>b) => a will be placed before b
  • zero value (a == b) => unchanged
  • Positive values ​​(a < b) => a will come after b

Example: This example uses the compare function to sort the array elements in ascending order.

<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>

output:

原始阵列
12,25,31,23,75,81,100

升序排序后
12,23,25,31,75,81,100

Now, we want to sort the array in descending order without changing the comparison function.

syntax:

函数(a,b){返回b-a}
  • Negative values ​​(b < a) => a will come after b
  • zero value (a == b) => unchanged
  • Positive values ​​(b > a) => a will come before b

Example: This example uses the compare function to sort the elements of an array in descending order.

<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>

output:

原始阵列
12,25,31,23,75,81,100

升序排序后
100,81,75,31,25,23,12

Creating loops: We can also use loops to sort array elements. Here, we will use bubble sort (simple sorting technique) to sort the array elements in ascending order. example:

<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>

output:

原始阵列
1,15,10,45,27,100

排序数组
1,10,15,27,45,100

Article from: Sorting an Array of Numbers with JavaScript

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324153873&siteId=291194637