From the course: Building Website Interactivity with JavaScript

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Sorting arrays in JavaScript

Sorting arrays in JavaScript

- [Instructor] In this video, we're going to try sorting an array without any customization and then learn about comparison functions that will let us customize the behavior of the sort. JavaScript arrays have a couple of methods that can be used for sorting. The one that's been around forever is just simply, array.sort. And there's some very good documentation on this page that will explain how the sort works in a lot of detail. There's a newer method called to sorted. The difference between these two is that sort will sort an array in place as the page says, so your original array is going to be changed to the sorted version, whereas to sorted will return a new array where the data are sorted. We're going to try out first just doing a basic sort without doing anything special. So we're going to take our table data and just sort it. We're just going to call sort just like that and then we'll log to the console what we end up with. So we'll say data sorted here and include our table…

Contents