| @@ 536-552 (lines=17) @@ | ||
| 533 | * |
|
| 534 | * @return Map |
|
| 535 | */ |
|
| 536 | public function sorted(callable $comparator = null): Map |
|
| 537 | { |
|
| 538 | $sorted = new self($this); |
|
| 539 | ||
| 540 | if ($comparator) { |
|
| 541 | usort($sorted->pairs, function($a, $b) use ($comparator) { |
|
| 542 | return $comparator($a->value, $b->value); |
|
| 543 | }); |
|
| 544 | ||
| 545 | } else { |
|
| 546 | usort($sorted->pairs, function($a, $b) { |
|
| 547 | return $a->value <=> $b->value; |
|
| 548 | }); |
|
| 549 | } |
|
| 550 | ||
| 551 | return $sorted; |
|
| 552 | } |
|
| 553 | ||
| 554 | /** |
|
| 555 | * Sorts the map in-place, based on an optional callable comparator. |
|
| @@ 583-599 (lines=17) @@ | ||
| 580 | * |
|
| 581 | * @return Map |
|
| 582 | */ |
|
| 583 | public function ksorted(callable $comparator = null): Map |
|
| 584 | { |
|
| 585 | $sorted = $this->copy(); |
|
| 586 | ||
| 587 | if ($comparator) { |
|
| 588 | usort($sorted->pairs, function($a, $b) use ($comparator) { |
|
| 589 | return $comparator($a->key, $b->key); |
|
| 590 | }); |
|
| 591 | ||
| 592 | } else { |
|
| 593 | usort($sorted->pairs, function($a, $b) { |
|
| 594 | return $a->key <=> $b->key; |
|
| 595 | }); |
|
| 596 | } |
|
| 597 | ||
| 598 | return $sorted; |
|
| 599 | } |
|
| 600 | ||
| 601 | /** |
|
| 602 | * Returns the sum of all values in the map. |
|