0

Noob question. So, I am sorting an array of numbers in ascending order, but I don't understand why this works.

const sortYears = arr => {
  arr = (a, b) => {return a - b;}
}

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

console.log(years.sort(sortYears()));

What does a - b actually achieve? Is it not just returning another number? How is it suddenly able to sort these numbers?

Ian C.
  • 78
  • 7
  • 1
    It is not sudden, that's how it's supposed to work. Check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Addis Jan 10 '20 at 20:57
  • 1
    If the result is zero, the items are equal, if its negative or positive determine if it's greater or lesser. That's how it works. – Elias Soares Jan 10 '20 at 20:57

3 Answers3

1

The effect of the number produced by a - b or b - a is in relation to 0:

  1. b === a - the items' order respective to each other won't change
  2. b - a < 0 - move a to a lower index than b
  3. b - a > 0 - move b to a lower index than a

However, the sortYears function returns undefined, so it's not taken into account. Although the default sort is ascending. It created by converting the items to strings, and sorting them as strings. Supplying a compare function will ensure that they are sorted as numbers (note the placement of the number 2 using the default compare):

const sortYears = () => (a, b) => a - b;

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

console.log(years.sort(sortYears())); // with compare function
// [2,1922,1951,1963,1970,1982,1999,2011,2018]

console.log([...years].sort()); // using the default compare
// [1922,1951,1963,1970,1982,1999,2,2011,2018]
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • Thanks. I actually didn't mean to type it up that way and I edited the question. But this helps me understand why it wasn't in descending order as I originally intended. – Ian C. Jan 10 '20 at 21:03
  • I've updated the answer to explain why `a - b` is different than the default compare. – Ori Drori Jan 10 '20 at 21:10
1

Take any two years, say 1970 and 1963. 1970 - 1963 gives you a positive number. That means 1970 goes after 1963 in the list. 1963 - 1970 gives you a negative number, so 1963 goes before 1970 in the list. It's the same as this:

 const sortYears = arr => {
    arr = (a, b) => {
        if(a - b > 0){
            return 1
        }else if (a - b < 0){
            return -1
        }else if(a - b === 0){
            return 0
        }
    }
}
symlink
  • 11,984
  • 7
  • 29
  • 50
1

By default, the sort() method sorts the values as strings in alphabetical and ascending order. To work well with numbers you need to provide a compare function to define an alternative sort order logic. The function should return a negative, zero, or positive value.

Inspecting your function:

const sortYears = () => (a, b) => b - a;

If, depending of the arguments, it returns:

  • negative: the b value is the lower value
  • positive: the b value is the greater value
  • zero: both values are equal

Hope that helps.

racsoraul
  • 196
  • 1
  • 8