April 2018
Beginner to intermediate
426 pages
10h 19m
English
Throughout this book, you will learn how to write the most-used searching and sorting algorithms. However, JavaScript also has a sorting method and a couple of search methods available. Let's take a look at them.
First, let's take our numbers array and put the elements out of order (1, 2, 3, ... 15 are already sorted). To do this, we can apply the reverse method, in which the last item will be the first and vice versa, as follows:
numbers.reverse();
So now, the output for the numbers array will be [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]. Then, we can apply the sort method as follows:
numbers.sort();
However, if we output the array, the result will be [1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9]. This ...