| @@ 516-532 (lines=17) @@ | ||
| 513 | * |
|
| 514 | * @return Map |
|
| 515 | */ |
|
| 516 | public function sorted(callable $comparator = null): Map |
|
| 517 | { |
|
| 518 | $sorted = new self($this); |
|
| 519 | ||
| 520 | if ($comparator) { |
|
| 521 | usort($sorted->internal, function($a, $b) use ($comparator) { |
|
| 522 | return $comparator($a->value, $b->value); |
|
| 523 | }); |
|
| 524 | ||
| 525 | } else { |
|
| 526 | usort($sorted->internal, function($a, $b) { |
|
| 527 | return $a->value <=> $b->value; |
|
| 528 | }); |
|
| 529 | } |
|
| 530 | ||
| 531 | return $sorted; |
|
| 532 | } |
|
| 533 | ||
| 534 | /** |
|
| 535 | * Sorts the map in-place, based on an optional callable comparator. |
|
| @@ 563-579 (lines=17) @@ | ||
| 560 | * |
|
| 561 | * @return Map |
|
| 562 | */ |
|
| 563 | public function ksorted(callable $comparator = null): Map |
|
| 564 | { |
|
| 565 | $sorted = $this->copy(); |
|
| 566 | ||
| 567 | if ($comparator) { |
|
| 568 | usort($sorted->internal, function($a, $b) use ($comparator) { |
|
| 569 | return $comparator($a->key, $b->key); |
|
| 570 | }); |
|
| 571 | ||
| 572 | } else { |
|
| 573 | usort($sorted->internal, function($a, $b) { |
|
| 574 | return $a->key <=> $b->key; |
|
| 575 | }); |
|
| 576 | } |
|
| 577 | ||
| 578 | return $sorted; |
|
| 579 | } |
|
| 580 | ||
| 581 | /** |
|
| 582 | * Returns the sum of all values in the map. |
|