Now let us try to create an array and then display them to see the elements and their order. After this we will apply sort() function and then again we will display the elements of the array to check the order. Here is the code.
The function will accept two arguments and these arguments are to be compared for the algorithm. The function will return numbers based on the comparison of the two arguments.
If the returned number is < 0 then first argument should appear before the second argument.
If the returned number is greater than 0 then first argument should appear after the second argument.
If the returned number is equal to 0 then both the arguments should be equal.
Let us try to sort the array elements by their lengths using the function. Here is the code Here is the code.
Be the first to post comment on this article :