* Returns a comparison result between %%this%% and %%other%%. * * This is suitable for use in sorting, where ``-1`` implies %%this%% * is smaller, ``1`` implies %%this%% is larger and ``0`` implies * both are equal.
(other)
| 336 | * both are equal. |
| 337 | */ |
| 338 | cmp(other) { |
| 339 | let a = this.value, b = other.value; |
| 340 | // Coerce a and b to the same magnitude |
| 341 | const delta = this.decimals - other.decimals; |
| 342 | if (delta > 0) { |
| 343 | b *= getTens(delta); |
| 344 | } |
| 345 | else if (delta < 0) { |
| 346 | a *= getTens(-delta); |
| 347 | } |
| 348 | // Comnpare |
| 349 | if (a < b) { |
| 350 | return -1; |
| 351 | } |
| 352 | if (a > b) { |
| 353 | return 1; |
| 354 | } |
| 355 | return 0; |
| 356 | } |
| 357 | /** |
| 358 | * Returns true if %%other%% is equal to %%this%%. |
| 359 | */ |