| @@ 522-538 (lines=17) @@ | ||
| 519 | * |
|
| 520 | * @return Map |
|
| 521 | */ |
|
| 522 | public function sort(callable $comparator = null): Map |
|
| 523 | { |
|
| 524 | $sorted = new self($this); |
|
| 525 | ||
| 526 | if ($comparator) { |
|
| 527 | usort($sorted->pairs, function($a, $b) use ($comparator) { |
|
| 528 | return $comparator($a->value, $b->value); |
|
| 529 | }); |
|
| 530 | ||
| 531 | } else { |
|
| 532 | usort($sorted->pairs, function($a, $b) { |
|
| 533 | return $a->value <=> $b->value; |
|
| 534 | }); |
|
| 535 | } |
|
| 536 | ||
| 537 | return $sorted; |
|
| 538 | } |
|
| 539 | ||
| 540 | /** |
|
| 541 | * Returns a sorted copy of the map, based on an optional callable |
|
| @@ 548-564 (lines=17) @@ | ||
| 545 | * |
|
| 546 | * @return Map |
|
| 547 | */ |
|
| 548 | public function ksort(callable $comparator = null): Map |
|
| 549 | { |
|
| 550 | $sorted = clone $this; |
|
| 551 | ||
| 552 | if ($comparator) { |
|
| 553 | usort($sorted->pairs, function($a, $b) use ($comparator) { |
|
| 554 | return $comparator($a->key, $b->key); |
|
| 555 | }); |
|
| 556 | ||
| 557 | } else { |
|
| 558 | usort($sorted->pairs, function($a, $b) { |
|
| 559 | return $a->key <=> $b->key; |
|
| 560 | }); |
|
| 561 | } |
|
| 562 | ||
| 563 | return $sorted; |
|
| 564 | } |
|
| 565 | ||
| 566 | /** |
|
| 567 | * @inheritDoc |
|