FAQ: Code Challenges: Intermediate JavaScript - sortYears()

This community-built FAQ covers the “sortYears()” exercise from the lesson “Code Challenges: Intermediate JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise sortYears()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

const sortYears = arr => arr.sort((x, y) => y - x)

I don’t understand what " (x, y) => y - x " is doing, why would it return a year and not the value of y minus x?


I want to know that too, I don’t understand that… Can some one explain how does it work?

1 Like

because of how sort works, here is useful documentation:

we pass our own function to the sort method to determine sorting order. The result of y - x is then used to determine were the element should be sorted. for example, the first two elements in our array are:

1970, 1999

so if we then fill in our function which determine sorting order:

(1970, 1999) => 1999 - 1970

which gives a a value higher then 0, which means y comes before x.

in short, the .sort() method will call your function to determine sorting order.

1 Like

Something to consider would be:

const sortYears = arrYears => {
  return arrYears.sort().reverse();

const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]

I suggest you first
console.log(years.sort()) ( inside the function it’s console.log(arrYears.sort()) )
you’ll notice the numbers in the array are descending from left to right.
then chain the reverse() method to log out the numbers ascending from left to right (in the correct order).

true, but sorting them in the correct order in the first place is very likely more efficient. Programming is always a tricky balance (performance vs readability vs maintainability)

How does y-x work to define the sort order?

You have (1970, 1999) => 1999 - 1970, but how are the rest of the years impacted?

Mainly, I’m confused about the output that checkYears produces, for sort() to use. Does checkYears go one by one through all the entire array? Like 1999 - 1970, 1951 - 1999, 1982 - 1951, 1963 - 1982, etc.

From the solution:

// As a function declaration AND using a named function:
function sortYears(arr) {
const checkYears = (year1, year2) => year2 - year1
return arr.sort(checkYears)

the .sort() method implement logic under the hood, as explained here:


Yeah so I’ve literally solved this and am still getting marked wrong despite the fact that we aren’t told HOW this needs to be solved beyond using .sort(). Here’s my code:

The solution didn’t make sense to me either and even though I’ll read up on it, I’m frustrated that I’m getting marked wrong for how I went about getting my answer.