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?

2 Likes

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.

2 Likes

Something to consider would be:

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

const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]
console.log(sortYears(years))

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).

1 Like

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:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description

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.

1 Like

It is showing wrong because of the instruction of the intermediate JavaScript Practice: "You’ll have to pass in your own function in .sort() method to get the functionality you want. "

That feeling :see_no_evil: when u spend one hour to try to figure out the response and suddenly u find out that u need to complete it in 2 lines of code.

I referenced a different article regarding the .sort descending issue, after understanding what I needed to do, my output was this:

// Write your code here:


//callback function to sort array output in descending order
function sortDescending(a, b) {
  return a > b ? -1 : b > a ? 1 : 0;
}

// function to return array items
const sortYears = arr => {
  return years.sort(sortDescending); // uses .sort method and calls sortDescending to arrange elements in descending order
}





// Feel free to uncomment the below code to test your function:

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

console.log(sortYears(years))
// Should print [ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ]


According to my console the output is correct, but the lesson isnt marked off as checked. Any ideas why?

the error is a good hint:

Does your function call sort() on the array that was passed into it?

where it means the sortYears function. If you added another array:

const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]
const temp = [1988, 1977, 1999, 2111, 2222, 1966]

console.log(sortYears(years))
console.log(sortYears(temp))

i do not get the desired output for my temp array.

ahh I see, dammit same mistake I made a few lessons back.

changed

return years.sort(sortDescending);

to

return arr.sort(sortDescending);

all is well. Thanks!

1 Like

God this exercise is killing me… youtube videos here i come