Skip to content

Commit

Permalink
Added toSorted() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Oct 23, 2024
1 parent d32d40e commit bab88f9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ will return:
<a href="#slice">slice</a>
<a href="#some">some</a>
<a href="#sort">sort</a>
<a href="#sorted">sorted</a>
<a href="#splice">splice</a>
<a href="#split">split</a>
<a href="#strafter">strAfter</a>
Expand All @@ -262,6 +263,7 @@ will return:
<a href="#times">times</a>
<a href="#toarray">toArray</a>
<a href="#tojson">toJson</a>
<a href="#tosorted">toSorted</a>
<a href="#tourl">toUrl</a>
<a href="#transform">transform</a>
<a href="#transpose">transpose</a>
Expand Down Expand Up @@ -5385,6 +5387,25 @@ Map::from( ['a', 'b'] )->toJson( JSON_FORCE_OBJECT );
```


### toSorted()

Sorts the elements in a copy of the map using new keys (alias).

```php
public function toSorted( int $options = SORT_REGULAR ) : self
```

* @param **int** `$options` Sort options for PHP `sort()`
* @return **self&#60;int&#124;string,mixed&#62;** New map with a sorted copy of the elements

This method is an alias for [sorted()](#sorted). For performance reasons, sorted() should be
preferred because it uses one method call less than toSorted().

**See also:**

* [sorted()](#sorted) - Underlying method with same parameters and return value but better performance


### toUrl()

Creates a HTTP query string from the map elements.
Expand Down
16 changes: 16 additions & 0 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -4906,6 +4906,22 @@ public function toJson( int $options = 0 ) : ?string
}


/**
* Sorts the elements in a copy of the map using new keys.
*
* This method is an alias for sorted(). For performance reasons, sorted() should be
* preferred because it uses one method call less than toSorted().
*
* @param int $options Sort options for PHP sort()
* @return self<int|string,mixed> New map with a sorted copy of the elements
* @see sorted() - Underlying method with same parameters and return value but better performance
*/
public function toSorted( int $options = SORT_REGULAR ) : self
{
return $this->sorted( $options );
}


/**
* Creates a HTTP query string from the map elements.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3215,6 +3215,17 @@ public function testTimesObjects()
}


public function testToSorted()
{
$m = new Map( [-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4] );
$n = $m->toSorted();

$this->assertNotSame( $n, $m );
$this->assertInstanceOf( Map::class, $n );
$this->assertSame( [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], $n->toArray() );
}


public function testTransform()
{
$m = Map::from( ['a' => 2, 'b' => 4] )->transform( function( $value, $key ) {
Expand Down

0 comments on commit bab88f9

Please sign in to comment.