-1

javascript newbie here. I was practicing JavaScript30 by Wes Bos, the question was to sort the array by the years in ascending order. This was the answer that he provided:

 const inventors = [
      { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
      { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
      { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
      { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }
    ];
var age = inventors.sort(function(a,b){
if (a.year>b.year){
    return 1;
}else{
    return -1;}
});
console.log(age);

Can someone explain to me, how does the return 1 and return -1 work?

 const inventors = [
      { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
      { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
      { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
      { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 }
    ];
var age = inventors.sort(function(a,b){
if (a.year>b.year){
    return a;
}else{
    return b;}
});
console.log(age);

^ This was the answer I came out with, but it doesn't sort the array based on their years, and im not sure why

bunkgod2
  • 9
  • 2
  • Read the specs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – jabaa Feb 13 '23 at 10:42
  • *"Can someone explain to me, how does the return 1 and return -1 work?"* It doesn't, reliably. That `sort` callback (both of them, actually) is incorrect. It should return a negative number if `a` should come before `b`, `0` if `a` and `b` are the same for sorting purposes, or a positive number if `a` should come after `b`. Instead, the first one above is returning `1` when `a` and `b` are the same. The correct version, assuming the `year` is reliably a number, is `return a.year - b.year;`. See: https://stackoverflow.com/questions/24768492/ – T.J. Crowder Feb 13 '23 at 10:45
  • @T.J.Crowder Do you know whether the `1` for equal `a` and `b` actually causes a problem? I.e., is there an input where that goes wrong, not leading to a correctly sorted array? – Kelly Bundy Feb 14 '23 at 09:49
  • 1
    @KellyBundy - I saw an example once where it led to an incorrect sort result, several years ago; I didn't keep the example. But the callback is expected to return `0` for a reason. If the callback returns a non-zero value for equivalent elements, I wouldn't be surprised to get an incorrect result, or for the `sort` to take longer than necessary, or even for the `sort` never to return (for instance, if it gets stuck in a cycle where it gives the callback `x, y` for `a, b` and gets back 1, then later does `y, x` for `a, b` and gets back 1). All bets are off when you break the contract. :-) – T.J. Crowder Feb 14 '23 at 10:07

1 Answers1

-1

the sort() function can accept a comparator compareFn(a,b), whick tells you whether to put a after b. When a should be put after b, the comparator should return a value > 0. And When a should be put before b, it should return a value < 0.

For more info, you can refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort