Skip to content

Commit

Permalink
Added uksorted() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Nov 23, 2024
1 parent 822794e commit a508b37
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ will return:
<a href="#uasort">uasort</a>
<a href="#uasorted">uasorted</a>
<a href="#uksort">uksort</a>
<a href="#uksorted">uksorted</a>
<a href="#union">union</a>
<a href="#unique">unique</a>
<a href="#unshift">unshift</a>
Expand Down Expand Up @@ -388,6 +389,7 @@ will return:
* [uasort()](#uasort) : Sorts elements preserving keys using callback
* [uasorted()](#uasorted) : Sorts elements preserving keys using callback in a copy of the map
* [uksort()](#uksort) : Sorts elements by keys using callback
* [uksorted()](#uksorted) : Sorts elements by keys using callback in a copy of the map
* [usort()](#usort) : Sorts elements using callback assigning new keys

### Shorten
Expand Down Expand Up @@ -6021,6 +6023,37 @@ Map::from( ['B' => 'a', 'a' => 'b'] )->uksort( function( $keyA, $keyB ) {
```


### uksorted()

Sorts a copy of the map elements by their keys using a callback.

```php
public function uksorted( callable $callback ) : self
```

* @param **callable** `$callback` Function with (keyA, keyB) parameters and returns -1 (<), 0 (=) and 1 (>)
* @return **self&#60;int&#124;string,mixed&#62;** Updated map for fluid interface

The given callback will be used to compare the keys. The callback must accept
two parameters (key A and B) and must return -1 if key A is smaller than
key B, 0 if both are equal and 1 if key A is greater than key B. Both, a
method name and an anonymous function can be passed.

The keys are preserved using this method and no new map is created.

**Examples:**

```php
Map::from( ['B' => 'a', 'a' => 'b'] )->uksorted( 'strcasecmp' );
// ['a' => 'b', 'B' => 'a']

Map::from( ['B' => 'a', 'a' => 'b'] )->uksorted( function( $keyA, $keyB ) {
return strtolower( $keyA ) <=> strtolower( $keyB );
} );
// ['a' => 'b', 'B' => 'a']
```


### union()

Builds a union of the elements and the given elements without returning a new map.
Expand Down
29 changes: 29 additions & 0 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -5485,6 +5485,35 @@ public function uksort( callable $callback ) : self
}


/**
* Sorts a copy of the map elements by their keys using a callback.
*
* The given callback will be used to compare the keys. The callback must accept
* two parameters (key A and B) and must return -1 if key A is smaller than
* key B, 0 if both are equal and 1 if key A is greater than key B. Both, a
* method name and an anonymous function can be passed.
*
* Examples:
* Map::from( ['B' => 'a', 'a' => 'b'] )->uksorted( 'strcasecmp' );
* Map::from( ['B' => 'a', 'a' => 'b'] )->uksorted( function( $keyA, $keyB ) {
* return strtolower( $keyA ) <=> strtolower( $keyB );
* } );
*
* Results:
* ['a' => 'b', 'B' => 'a']
* ['a' => 'b', 'B' => 'a']
*
* The keys are preserved using this method and no new map is created.
*
* @param callable $callback Function with (keyA, keyB) parameters and returns -1 (<), 0 (=) and 1 (>)
* @return self<int|string,mixed> Updated map for fluid interface
*/
public function uksorted( callable $callback ) : self
{
return ( clone $this )->uksort( $callback );
}


/**
* Builds a union of the elements and the given elements without overwriting existing ones.
* Existing keys in the map will not be overwritten
Expand Down
13 changes: 13 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3586,6 +3586,19 @@ public function testUksort()
}


public function testUksorted()
{
$m = new Map( ['a' => 'foo', 'c' => 'bar-10', 1 => 'bar-1'] );
$r = $m->uksorted( function( $a, $b ) {
return (string) $a <=> (string) $b;
} );

$this->assertNotSame( $r, $m );
$this->assertInstanceOf( Map::class, $r );
$this->assertSame( [1 => 'bar-1', 'a' => 'foo', 'c' => 'bar-10'], $r->toArray() );
}


public function testUsort()
{
$m = ( new Map( ['foo', 'bar-10', 'bar-1'] ) )->usort( function( $a, $b ) {
Expand Down

0 comments on commit a508b37

Please sign in to comment.