IdecEddy
IdecEddy

Reputation: 159

What does the multiplication operator do in this code?

So I saw this code and have no clue what the * in sortBy[i].direction*(...) does. Can anyone break this down for me and help me understand this code?

result = sortBy[i].direction*(a[ sortBy[i].prop ] < b[ sortBy[i].prop ] ? -1 : (a[ sortBy[i].prop ] > b[ sortBy[i].prop ] ? 1 : 0));

Upvotes: 0

Views: 53

Answers (1)

enzo
enzo

Reputation: 11496

The * operator multiples the value of sortBy[i].direction with

  • -1 if a[sortBy[i].prop] < b[sortBy[i].prop];

  • 1 if a[sortBy[i].prop] > b[sortBy[i].prop];

  • 0 otherwise.

Look at this as

result = sortBy[i].direction * 
  (a[sortBy[i].prop] < b[sortBy[i].prop] 
    ? -1
    : (a[sortBy[i].prop] > b[sortBy[i].prop] 
      ? 1
      : 0));

Upvotes: 5

Related Questions