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 - JavaScript Tutorial
From the course: Building Website Interactivity with 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Exploring an accessible data table2m 7s
-
(Locked)
Collecting table data for sorting4m 30s
-
(Locked)
Sorting arrays in JavaScript9m 53s
-
(Locked)
Writing a custom sort function8m 9s
-
(Locked)
Updating the HTML table with sorted data4m 49s
-
(Locked)
Writing a custom numerical sort function2m 49s
-
(Locked)
Challenge: Sorting a table of books2m 38s
-
(Locked)
Solution: Sorting a table of books3m 44s
-
-
-
-