Nothing Special   »   [go: up one dir, main page]

Sorting Arrays

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Sorting

Arrays
Sorting arrays
Here we have a numbers array.

Until now, we have only dealt with sorted arrays, in real life however, arrays often come
unsorted.

Here we have changed the order of the numbers array.

Let’s look at how to sort this.


The array.sort()
method
We have access to a sort
method, and what this method
does, is that it converts each
element into a string, then sorts
the elements in the array.

When we log the array to the


console, it will display the
sorted array.
reverse()
Another method often used with
the sort method, is the
array.reverse() method.

This method reverses the order of


elements in an array.

Here we log the result of reversing


the sorted array, which displays the
numbers in descending order.
array.sort()
But, if you have an array of objects, the
array.sort() method does not work by
default, so we have make some
This method makes sorting arrays of additions to this to make it work.
numbers or strings easy.
Sorting objects
Here we have an array called courses,
filled with course objects, and each course
object has name and id properties.

We want to sort the courses by name, so


that JavaScript comes before Node.js

Here we try to use the sort method, but, as


the console shows, this does not work.

How can we fix this problem?


array.sort()
The sort method accepts an optional argument, and this
arguments is a function that is used for comparison.

What the sort method does, is it takes two objects in the


array (which we will call a and b), and it compares them.

If they are in the right order, it will skip to the next two
elements, otherwise it will rearrange the elements.
Sorting objects
To sort the courses by name, we pass in a function for comparison.

This functions takes two parameters, which we will call a and b.

In this function, if a is less than b, we return -1, if a is greater than b, then


we return 1, and if they are equal, we should return 0.

* Note that we don’t use an else-if statement.

This is because the first condition is true, the return keyword will break out
of the function, so control flow won’t reach the second if statement.

This means there is no need for the else keyword, so we leave it out to make
our code cleaner.
Case Sensitivity
If we change the J in JavaScript to a
lowercase j, we see that Node.js appears
first.

Why does this happen?

Each character in a computer is internally


represented using a number
ASCII Table
If you Google search for ascii table, this
table will show up.

Ascii stands for American Standard Code


for Information Interchange.

In this table you can see the numeric


representation of each character.

Since lowercase j is 106, and uppercase N


is 78, we can see 78 is less than 106, that's
why Node came first.
Case Sensitivity
To solve this problem we should exclude case
sensitivity when comparing these names.

To do this, we can declare 2 constants, then it it to


name and apply either the toUpperCase or
toLowerCase method.

So whatever the name of the first course is, we're


going to convert it to uppercase or lowercase, and
we're going to do the same with the second course.

Now the sort algorithm works properly.

You might also like