From the course: Learning Functional Programming with JavaScript ES6+

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Sorting

Sorting - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript ES6+

Start my 1-month free trial

Sorting

- [Instructor] Now that we've seen how to use Slice to prevent mutation, let's take a look at the function Sort. As you may have guessed by the name, this function is used when you want to change the order of elements in an array. This would be very useful, for example, if we had an array of names and wanted to sort it in alphabetical order. Or if we had an array of employees and wanted to sort them by the number of years they've been with the company, for example. Before we look at how to use sort, it's very important to remember that Sort is a mutating function. Which means that it actually modifies the original version of the array that it's called on. In a previous video we saw how JavaScript's built-in Slice function could be used to make a copy of an array, which would then allow us to use mutating functions such as Sort without modifying the original array. And that's exactly what we're going to do with Sort. So, whenever we use Sort instead of simply calling it using…

Contents