Sorting It Out

When dealing with objects and data, there are times you need to put the data in a specific order, such as alphabetical. There are various ways to go about sorting.

You could loop through an array, for example, and do some comparisons and swap data around as needed. So, while this is possible, it can be complex and prone to errors. Thankfully, JavaScript helps us out with a built-in sorting method…sort().

Note: keep data type in mind when sorting. For example, dates. Is a date an actual date or a string representation of a date? Comparing dates can sometimes be tricky. JavaScript has a date object method to create dates, which represents the date in milliseconds.

Here is an example…imagine you have the following array…

// Create animals array
var animals = ["Lions", "Tigers", "Bears", "Badgers"];
// Output array to the console
console.log(animals);

Here is a screenshot of the output…

array example
array example

Now let’s sort our animals…

// Create animals array
var animals = ["Lions", "Tigers", "Bears", "Badgers"];
// Sort the array of animals
animals.sort();
// Output array to the console
console.log(animals);

Here is the result…

sorted array
sorted array

As you can see, sorting is fairly straightforward. You should know that the sort method can take an optional parameter. The optional parameter would be a compare function to perform custom sorting. In other words, you can define an alternative sort order. Whatever compare function gets passed in, it needs to return a negative, positive or zero value. This tells the sort method how to order the items.

// Compare function
function(a, b){
    return a-b
}

Using a compare function like above, the sort method will compare the two values. It then sends the values to the compare function, sorts the values according to the returned value (negative. positive or zero).

So imagine you are comparing two numbers…5 & 10. In a regular sorting scenario, you would have 5 come before 10. The sort function gets passed the numbers (5 & 10), does a simple subtraction (5 – 10 = -5) and sees that it is a negative value. So 5 should come before 10.

Happy Coding!

Clay Hess

More To Explore

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

lighthouse, beacon, atlantic-8578318.jpg
Code

Understanding the Beacon API: Simplifying Asynchronous Data Transfers

In today’s data-driven world, web applications often need to send data back to the server. Traditionally, this has been done using AJAX requests or similar methods. However, these techniques can come with a cost, especially when dealing with data that needs to be sent during the unload phase of a document, such as tracking and diagnostic data. This is where the Beacon API shines by allowing developers to send data to a server more reliably and efficiently.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.