Skip to content

Commit

Permalink
Added rsorted() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Nov 22, 2024
1 parent eaac498 commit b952262
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ will return:
<a href="#reverse">reverse</a>
<a href="#reversed">reversed</a>
<a href="#rsort">rsort</a>
<a href="#rsorted">rsorted</a>
<a href="#rtrim">rtrim</a>
<a href="#search">search</a>
<a href="#sep">sep</a>
Expand Down Expand Up @@ -377,6 +378,7 @@ will return:
* [reversed()](#reversed) : Reverses the element order in a copy of the map
* [toReversed()](#toreversed) : Reverses the element order in a copy of the map (alias)
* [rsort()](#rsort) : Reverse sort elements using new keys
* [rsorted()](#rsorted) : Reverse sort elements using new keys in a copy of the map
* [shuffle()](#shuffle) : Randomizes the element order
* [shuffled()](#shuffled) : Randomizes the element order in a copy of the map
* [sort()](#sort) : Sorts the elements in-place assigning new keys
Expand Down Expand Up @@ -4412,6 +4414,44 @@ Map::from( [0 => 'C', 1 => 'b'] )->rsort( SORT_STRING|SORT_FLAG_CASE );
```


### rsorted()

Sorts a copy of all elements in reverse order without maintaining the key association.

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

* @param **int** `$options` Sort options for `rsort()`
* @return **self&#60;int&#124;string,mixed&#62;** Updated map for fluid interface

The parameter modifies how the values are compared. Possible parameter values are:
- SORT_REGULAR : compare elements normally (don't change types)
- SORT_NUMERIC : compare elements numerically
- SORT_STRING : compare elements as strings
- SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by `setlocale()`
- SORT_NATURAL : compare elements as strings using "natural ordering" like `natsort()`
- SORT_FLAG_CASE : use SORT_STRING&#124;SORT_FLAG_CASE and SORT_NATURAL&#124;SORT_FLAG_CASE to sort strings case-insensitively

The keys are NOT preserved and elements get a new index. No new map is created.

**Examples:**

```php
Map::from( ['a' => 1, 'b' => 0] )->rsorted();
// [0 => 1, 1 => 0]

Map::from( [0 => 'b', 1 => 'a'] )->rsorted();
// [0 => 'b', 1 => 'a']

Map::from( [0 => 'C', 1 => 'b'] )->rsorted();
// [0 => 'b', 1 => 'C']

Map::from( [0 => 'C', 1 => 'b'] )->rsorted( SORT_STRING|SORT_FLAG_CASE );
// [0 => 'C', 1 => 'b'] because 'C' -> 'c' and 'c' > 'b'
```


### rtrim()

Removes the passed characters from the right of all strings.
Expand Down
30 changes: 30 additions & 0 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -3899,6 +3899,36 @@ public function rsort( int $options = SORT_REGULAR ) : self
}


/**
* Sorts a copy of all elements in reverse order using new keys.
*
* Examples:
* Map::from( ['a' => 1, 'b' => 0] )->rsorted();
* Map::from( [0 => 'b', 1 => 'a'] )->rsorted();
*
* Results:
* [0 => 1, 1 => 0]
* [0 => 'b', 1 => 'a']
*
* The parameter modifies how the values are compared. Possible parameter values are:
* - SORT_REGULAR : compare elements normally (don't change types)
* - SORT_NUMERIC : compare elements numerically
* - SORT_STRING : compare elements as strings
* - SORT_LOCALE_STRING : compare elements as strings, based on the current locale or changed by setlocale()
* - SORT_NATURAL : compare elements as strings using "natural ordering" like natsort()
* - SORT_FLAG_CASE : use SORT_STRING|SORT_FLAG_CASE and SORT_NATURALSORT_FLAG_CASE to sort strings case-insensitively
*
* The keys aren't preserved and elements get a new index. No new map is created
*
* @param int $options Sort options for rsort()
* @return self<int|string,mixed> Updated map for fluid interface
*/
public function rsorted( int $options = SORT_REGULAR ) : self
{
return ( clone $this )->rsort( $options );
}


/**
* Removes the passed characters from the right of all strings.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,17 @@ public function testReversed()
}


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

$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 testRsortNummeric()
{
$m = ( new Map( [-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4] ) )->rsort();
Expand Down

0 comments on commit b952262

Please sign in to comment.