| @@ 510-526 (lines=17) @@ | ||
| 507 | * |
|
| 508 | * @return Map |
|
| 509 | */ |
|
| 510 | public function sort(callable $comparator = null): Map |
|
| 511 | { |
|
| 512 | $sorted = new self($this); |
|
| 513 | ||
| 514 | if ($comparator) { |
|
| 515 | usort($sorted->pairs, function($a, $b) use ($comparator) { |
|
| 516 | return $comparator($a->value, $b->value); |
|
| 517 | }); |
|
| 518 | ||
| 519 | } else { |
|
| 520 | usort($sorted->pairs, function($a, $b) { |
|
| 521 | return $a->value <=> $b->value; |
|
| 522 | }); |
|
| 523 | } |
|
| 524 | ||
| 525 | return $sorted; |
|
| 526 | } |
|
| 527 | ||
| 528 | /** |
|
| 529 | * Returns a sorted copy of the map, based on an optional callable |
|
| @@ 536-552 (lines=17) @@ | ||
| 533 | * |
|
| 534 | * @return Map |
|
| 535 | */ |
|
| 536 | public function ksort(callable $comparator = null): Map |
|
| 537 | { |
|
| 538 | $sorted = clone $this; |
|
| 539 | ||
| 540 | if ($comparator) { |
|
| 541 | usort($sorted->pairs, function($a, $b) use ($comparator) { |
|
| 542 | return $comparator($a->key, $b->key); |
|
| 543 | }); |
|
| 544 | ||
| 545 | } else { |
|
| 546 | usort($sorted->pairs, function($a, $b) { |
|
| 547 | return $a->key <=> $b->key; |
|
| 548 | }); |
|
| 549 | } |
|
| 550 | ||
| 551 | return $sorted; |
|
| 552 | } |
|
| 553 | ||
| 554 | /** |
|
| 555 | * @inheritDoc |
|