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