Sorting It Out

Share This Post

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

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
// Output array to the console

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


Demystifying Scrum User Stories Confirmation: Ensuring Quality and Collaboration

One of the key elements of Scrum is the use of user stories to define the requirements of a system or feature from the perspective of the end user. As teams work through the product backlog, it becomes crucial to confirm the user stories to ensure they meet the desired criteria and are ready for development. In this blog post, we’ll explore the concept of Scrum user stories confirmation and its significance in delivering high-quality products.


The Power of Conversations in Scrum User Stories

At the heart of Scrum lies the concept of user stories, which serve as a means of capturing requirements from the perspective of end-users. However, the true power of user stories lies not just in their written form but also in the conversations that take place around them. In this blog post, we will explore the significance of conversations in Scrum user stories and how they contribute to the success of Agile projects.

Do You Want To Boost Your Business?

drop us a line and keep in touch

Scroll to Top

Need help?

Let's have a chat...


Jump Back In!

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


Begin Your Learning Journey Today!

Come back inside to continue your learning journey.