CalvinTorra
Sort two Object properties with Array.sort()
IdontKnowJS

### Learn These 6 Figure Scraping Techniques As A Side Hustle

Access secret data points from SaaS websites and much more.

→ 6 Figure Scraping Guide ←

Use this data for your own startup or sell it to those that need it.

## No spam, just Javascript, web3 and indie hacking!

We won’t send you spam. Unsubscribe at any time.

Bit of a long one, but I had an array of objects that contained movie titles, movie release dates, reviews, directors etc.

The task was to sort them by release date in ascending order. In my head that was fine, just find the property by while running the Array.sort() method.

However if two movies had the same release date, these 2 or 3 movies would need to be further sorted by title in alphabetical order.

This stumped me for a while as I’ve never had to sort something twice in the same method call.

It was a little difficult to find a solution online as the question seemed to be a little difficult to come up with to get the right answer.

### Array.sort()

This method checks two elements and if the result of this check is a positive number, it places the second element before the first.

If the result of the check is negative, it keeps the first element in its place and puts the second after it.

> 0 returned? sort b before a
< 0 returned? sort a before b
=== 0 returned? keep original order of a and b

If the result is 0, this means they are eaqual. This is where our problem comes in. We have two movies with identical date values.

We now want to sort those alphabetically. The solutions turns out to be simple but took me a while staring at it to realise I never understood what sort was doing in the first place.

We want sort to do it’s job normally if the dates are NOT identical. So we check if a minus b is a positive or negative number and if it is, carry on as sort() normally does.

What I didn’t understand initially here is that the result doesn’t have to be 0, 1 or -1. It can be any positive and negative number…silly me.


let sortedAsc = myArrayOfObjects.sort((a, b) => {
let result = a.year - b.year;
// 1995 - 1987 = 8
// a - b will sort in ascending just like a is before b
// b - a would be backwards "think z -> a" a being last.

if (result !== 0) return result;
// if 8 is not equal to 0 we short circuit
// and return "positive" ie: sort b before a

// If we get to here, it means they're the same
// and we need another sorting check.

// String length and alphabet order seems to be hit by > or <.
// Aalvin is less than Calvin, which means Aalvin comes first.

if (a.title < b.title) {
return -1;
} else if (a.title > b.title) {
return 1;
} else {
return 0;
}
});



Sorting by letter’s is a slightly different check. It looks like we can’t use minus symbol but instead we’re looking at Greater or Less than symbols.

Javascript has its own way of evaluating the result of this. I believe it stops checking the moment it finds a difference in values within the string, this may mean the ENTIRE string is not checked. Just the first few characters until a difference is found.

We’re trying to return a positive, negative or 0 after checking the string order. So we use dot notation to get the value, compare them to each other and return a number to finish to original Array.sort() method.