FAQ: Code Challenges: Intermediate JavaScript - sortYears()

The exercise seems to expect you use the built-in .sort() method to do the sorting for you

sorting the data and then reversing is very likely less efficient

1 Like

hmm I did the same, I saw that .sort() by standard sort in ascending order, so I thought a .reverse() here would do the job perfectly, which it did.
And by further investigating the web this also seems to be the most accepted/ preferred way of dealing with this problem.

So I guess I’m happy with that :stuck_out_tongue:

If the data set you need to sort through is really large, and you need to consider performance, sorting in reverse might be faster then sorting and reversing

Here is the solution I came up with:

function sortYears (arr) { return arr.sort((a, b) => (b - a)); }

The default behavior for .sort() is sort ascending using (a - b) as the evaluation criteria. Reversing that criteria to (b - a) will give you a descending sort.
This feels preferable to sort then reverse as it doesn’t require you to touch the data twice.

Short 'n Sweet


1 Like

I think its a really late reply but here is the reasoning:
when sort function return a value lesser than 0 it means (y should be greater than X) and if it is greater than 0 then it means (x should be greater than y) so for reversing it simply means y - x (since y is at the moment greater than x so it will return a value greater than 0 to reverse the present order)
Sort can take compare function and we get x and y values by default from our array, depending on which item “sort” method is currently working on. i.e present element as x and next element as y.
Have a nice day, and keep coding.

1 Like

Why I’m getting undefined?

I have corrected myself by placing a return before arr thus ;
return arr. sort ()

Feeling so dumb…
could not figure out i would have to use x and y

// Write your code here:
const sortYears = yearsArr => yearsArr.sort();

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



this is all i could come out with.

is a for loop a solution as well? how?

Thanks for the time.

You could implement a sorting algorithm, the easiest of which is bubble sort:

Bubble Sort - GeeksforGeeks

then you need to implement the sorting, which will most certainly include a loop

Here is my solution. After carefully reading the documentation, everything worked out.

//additional fn to change the order of the elements
const compare = (a, b) => {   
  if(a > b){return -1}    // .sort() method accepts returned values -1 , 1 and 0.
  else if(a< b){return 1}
  else{return 0}

const sortYears = array => {
  return array.sort(compare);

After carefully reading the documentation, I’ve realized that this can be solved in just a few lines, and I guess this is the response that the platform is expecting from us:

function sortYears(array) {
  const sorted= array.sort((a, b) => b-a);
  return sorted;

The .sort method compares among values depending on the comparison results. Sort by himself will sort everything in ascending order, but will order based on a number: -1, 0, or 1. Sort() by himself will only order numbers based on the first digit. So [1, 200, 20, 100] would look [1, 100, 20, 200]. This is NOT the order we are expecting. To understand “which is bigger” you have to subtract two values and see the result. If 100 - 20 === (number-bigger-than-0), then 100 is bigger than 20, right?

This is what ((a, b) => b-a) is doing. Since you are ordering in ascending order by default, you can do the subtraction the other way around (b-a) so you can get a positive integer as a result & get the number 1 that the sort method needs to put “a” after “b”.

In the documentation, this is described better with this portion of code:

// functions
const numberArray = [40, 1, 5, 200];

function compareNumbers(a, b) {
  return a - b;

// invokes
numberArray.sort(); // expected output: [1, 200, 40, 5]
numberArray.sort(compareNumbers); // expected output: [1, 5, 40, 200]

You can see that they call compareNumbers, a function that subtracts the second item off the first item (a-b).

All I did was to switch positions (b-a).

Also, you don’t need to declare a function like compareNumbers, you can use a fat arrow shorthand like I did:

const sorted = array.sort((a, b) => b-a);

1 Like

Best explanation by far thank you!

1 Like

Nicely done! :sunglasses:

Mine looked like this initially, love your Concise Body Arrow version:

const sortYears = (arr) => {
  return arr.reverse();

Documentation had all the clues I needed:

const sortYears = arr => {
  const compareNumbers = (a, b) => b - a;
  return arr.sort(compareNumbers);

Am I the only one that feels like these are completely new ways of doing things and we’ve never learned this?


For some reason this isn’t giving me a green mark? Is it something that I’m missing? Because it does what it wants it to do?

// Write your code here:
const sortYears = arr => {
  let reversedYears = years.sort().reverse();
  return reversedYears;

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

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

// Should print [ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ]

The parameter of your function is arr.

Have a look at the body of your function. Are you sorting and reversing arr?

Suppose, we do something like,

//  Let's comment out/delete this line
// # const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]
// and replace it with:
const yrs = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]

// Does your function still work? 
// Why not? 

I think you are feeling this way because in the previous exercises we were getting step-by-step instructions on what to write, and we were not really told the logic behind these steps. What we need to learn and understand on our own are the principles. Personally, I found myself in the same situation but it just means checking my notes :slight_smile:

I was able to solve with

function sortYears(years) {
return years.sort().reverse();

should it have been that easy? seems like previous exercises took much more work and thinking.