sort()
MethodIf you simply try to sort a numeric array using the sort()
method it will not work, because the sort()
method sorts the array elements alphabetically. But, you can still sort an array of integers correctly through utilizing a compare function, as shown in the following example:
<script>
var numbers = [1, 5, 12, 3, 7, 15, 9];
// Sorting the numbers array simply using the sort method
numbers.sort(); // Sorts numbers array
alert(numbers); // Outputs: 1,12,15,3,5,7,9
/* Sorting the numbers array numerically in ascending order
using the sort method and a compare function */
numbers.sort(function(a, b){
return a - b;
});
alert(numbers); // Outputs: 1,3,5,7,9,12,15
</script>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to merge two or more arrays into one array in PHP
Use the PHP array_merge() func...How to Check If an Array Includes an Object in JavaScript
Use the JavaScript some() Method You...Laravel 8 Create Events in Fullcalendar using Ajax
FullCalendar is a open-source, lightweig...How to integrate Rozorpay Payment Gateway in Laravel 8
Today, I will share with you how to inte...How to Remove an Event Handler in jQuery
Use the jQuery off() Method You can s...