Introduction
Mutator Methods are methods that change the array objects. For example Unshift(), Shift(), Push(), Pop(), Reverse(), Sort() and Splice().
In my previous article, I already explained the Unshift(), Shift(), Push(), Pop() methods. So, please refer to them.
- Sort()- This method, as the name suggests, will sort the elements of an array in increasing (ascending) order by default.
- Reverse()- This method will reverse the functioning of the sort() method. In other words, it changes the sorting pattern to descending (decreasing) order.
- < script type = "text/javascript" >
- var myArray = ["Harry", "Tom", "Jerry", "Mike"];
- myArray.sort();
- document.write("The Sorted List in Ascending Order is: " + myArray + "<br/>");
- var myArray = ["Harry", "Tom", "Jerry", "Mike"];
- myArray.sort().reverse();
- document.write("The Sorted List in Descending Order is: " + myArray + "<br/>");
- < /script>
Now, if we change the elements with numeric values, it will not sort because it changes those values to a string and then sorts them. But, as we know, the numeric values cannot be sorted if they are strings.
So, we use the parameters in the sort function. The parameter of a sort function is also a function defined by the user.
Example

Join the conversation! Your thoughts help the community grow.