2

What does putting a minus sign in front of a function do?

For example:

return order === 'desc'
? (a, b) => descendingComparator(a, b, orderBy)
: (a, b) => -descendingComparator(a, b, orderBy);
Husso
  • 65
  • 3
  • It flips the positive/negative of the value. `var x = 1; -x; x = -1; -x;` <= put that in your console. – Taplar Sep 03 '20 at 16:40
  • 4
    This is one of those things where you can just try it and see. Make a function that returns a number. Run it and log the result. Put a minus sign in front of it. Run it and log the result. – Heretic Monkey Sep 03 '20 at 16:42
  • This is used to reverse the order of the sorting based on `order` value – adiga Sep 03 '20 at 16:44
  • Another way to do this would be to leave off the `-` and simply swap the order that `a, b` are passed into the second method. – Taplar Sep 03 '20 at 16:45
  • if `descendingComparator()` returns a number, I'd imagine the minus sign multiplies that number by -1 – TKoL Sep 03 '20 at 16:45

1 Answers1

1

It negates the value that descendingComparator(a, b, orderBy) returns, just like the - in -x negates the value that x contains. In context, what it's doing is reversing the order of the sort (presumably this is within an array sort callback or similar).

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • 1
    Basic questions are not necessarily *bad* questions. There's nothing wrong with answering them. In this case (as with many other quite basic questions), I decided to use a CW to answer it to avoid any appearance of having other motives than helping the OP. – T.J. Crowder Sep 03 '20 at 16:47