@@ 552-568 (lines=17) @@ | ||
549 | * |
|
550 | * @return Map |
|
551 | */ |
|
552 | public function sorted(callable $comparator = null): Map |
|
553 | { |
|
554 | $sorted = new self($this); |
|
555 | ||
556 | if ($comparator) { |
|
557 | usort($sorted->pairs, function($a, $b) use ($comparator) { |
|
558 | return $comparator($a->value, $b->value); |
|
559 | }); |
|
560 | ||
561 | } else { |
|
562 | usort($sorted->pairs, function($a, $b) { |
|
563 | return $a->value <=> $b->value; |
|
564 | }); |
|
565 | } |
|
566 | ||
567 | return $sorted; |
|
568 | } |
|
569 | ||
570 | /** |
|
571 | * Sorts the map in-place, based on an optional callable comparator. |
|
@@ 599-615 (lines=17) @@ | ||
596 | * |
|
597 | * @return Map |
|
598 | */ |
|
599 | public function ksorted(callable $comparator = null): Map |
|
600 | { |
|
601 | $sorted = $this->copy(); |
|
602 | ||
603 | if ($comparator) { |
|
604 | usort($sorted->pairs, function($a, $b) use ($comparator) { |
|
605 | return $comparator($a->key, $b->key); |
|
606 | }); |
|
607 | ||
608 | } else { |
|
609 | usort($sorted->pairs, function($a, $b) { |
|
610 | return $a->key <=> $b->key; |
|
611 | }); |
|
612 | } |
|
613 | ||
614 | return $sorted; |
|
615 | } |
|
616 | ||
617 | /** |
|
618 | * Returns the sum of all values in the map. |