Skip to content

Commit

Permalink
Add sort_by_priority
Browse files Browse the repository at this point in the history
  • Loading branch information
borkweb committed Aug 26, 2023
1 parent 074b8be commit 4029dbd
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ A library for array manipulations.
* [set](/docs/classes/StellarWP/Arrays/Arr.md#set)
* [shape_filter](/docs/classes/StellarWP/Arrays/Arr.md#shape_filter)
* [shuffle](/docs/classes/StellarWP/Arrays/Arr.md#shuffle)
* [sort_by_priority](/docs/classes/StellarWP/Arrays/Arr.md#sort_by_priority)
* [sort_recursive](/docs/classes/StellarWP/Arrays/Arr.md#sort_recursive)
* [sort_recursive_desc](/docs/classes/StellarWP/Arrays/Arr.md#sort_recursive_desc)
* [stringify_keys](/docs/classes/StellarWP/Arrays/Arr.md#stringify_keys)
Expand Down
67 changes: 60 additions & 7 deletions docs/classes/StellarWP/Arrays/Arr.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public static except(array $array, array|string|int|float $keys): array
Determine if the given key exists in the provided array.

```php
public static exists(\ArrayAccess|array $array, string|int $key): bool
public static exists(\ArrayAccess|array $array, string|int|float $key): bool
```


Expand All @@ -359,7 +359,7 @@ public static exists(\ArrayAccess|array $array, string|int $key): bool
| Parameter | Type | Description |
|-----------|------|-------------|
| `$array` | **\ArrayAccess|array** | |
| `$key` | **string|int** | |
| `$key` | **string|int|float** | |



Expand Down Expand Up @@ -431,7 +431,7 @@ public static first(iterable $array, callable|null $callback = null, mixed $defa
Flatten a multi-dimensional array into a single level.

```php
public static flatten(iterable $array, int $depth = INF): array
public static flatten(iterable $array, int $depth = PHP_INT_MAX): array
```

Typical use case is to flatten arrays like those returned by `get_post_meta( $id )`.
Expand Down Expand Up @@ -490,7 +490,7 @@ public static forget(array& $array, array|string|int|float $keys): void
Find a value inside of an array or object, including one nested a few levels deep.

```php
public static get(array|object|mixed $variable, array|string|int $indexes, mixed $default = null): mixed
public static get(array|object|mixed $variable, array|string|int|null $indexes, mixed $default = null): mixed
```

Example: get( $a, [ 0, 1, 2 ] ) returns the value of $a[0][1][2] or the default.
Expand All @@ -505,7 +505,7 @@ Example: get( $a, [ 0, 1, 2 ] ) returns the value of $a[0][1][2] or the default.
| Parameter | Type | Description |
|-----------|------|-------------|
| `$variable` | **array|object|mixed** | Array or object to search within. |
| `$indexes` | **array&#124;string&#124;int** | Specify each nested index in order.<br />Example: array( &#039;lvl1&#039;, &#039;lvl2&#039; ); |
| `$indexes` | **array&#124;string&#124;int&#124;null** | Specify each nested index in order.<br />Example: array( &#039;lvl1&#039;, &#039;lvl2&#039; ); |
| `$default` | **mixed** | Default value if the search finds nothing. |


Expand Down Expand Up @@ -586,7 +586,7 @@ The value of the specified index or the default if not found.
Check if an item or items exist in an array using "dot" notation.

```php
public static has(\ArrayAccess|array $array, array|string|int $indexes): bool
public static has(\ArrayAccess|array $array, array|string|int|null $indexes): bool
```


Expand All @@ -601,7 +601,7 @@ public static has(\ArrayAccess|array $array, array|string|int $indexes): bool
| Parameter | Type | Description |
|-----------|------|-------------|
| `$array` | **\ArrayAccess&#124;array** | |
| `$indexes` | **array&#124;string&#124;int** | The indexes to search; in order the function will look from the first to the last. |
| `$indexes` | **array&#124;string&#124;int&#124;null** | The indexes to search; in order the function will look from the first to the last. |



Expand Down Expand Up @@ -1243,6 +1243,59 @@ public static shuffle(array $array, int|null $seed = null): array



***

### sort_by_priority

Sort based on Priority

```php
public static sort_by_priority(array $array): int
```



* This method is **static**.




**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `$array` | **array** | Array to sort. |




***

### sort_by_priority_comparison

Sort based on Priority

```php
protected static sort_by_priority_comparison(object|array $a, object|array $b): int
```



* This method is **static**.




**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `$a` | **object&#124;array** | First Subject to compare |
| `$b` | **object&#124;array** | Second subject to compare |




***

### sort_recursive
Expand Down
54 changes: 54 additions & 0 deletions src/Arrays/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,60 @@ public static function shuffle( $array, $seed = null ) {
return $array;
}

/**
* Sort based on Priority
*
* @since 1.0.0
*
* @param array $array Array to sort.
*
* @return int
*/
public static function sort_by_priority( $array ): array {
if ( ! is_array( $array ) ) {
return $array;
}

if ( static::is_assoc( $array ) ) {
uasort( $array, [ static::class, 'sort_by_priority_comparison' ] );
} else {
usort( $array, [ static::class, 'sort_by_priority_comparison' ] );
}

return $array;
}

/**
* Sort based on Priority
*
* @since 1.0.0
*
* @param object|array $b Second subject to compare
*
* @param object|array $a First Subject to compare
*
* @return int
*/
protected static function sort_by_priority_comparison( $a, $b ): int {
if ( is_array( $a ) ) {
$a_priority = $a['priority'];
} else {
$a_priority = $a->priority;
}

if ( is_array( $b ) ) {
$b_priority = $b['priority'];
} else {
$b_priority = $b->priority;
}

if ( (int) $a_priority === (int) $b_priority ) {
return 0;
}

return (int) $a_priority > (int) $b_priority ? 1 : -1;
}

/**
* Recursively sort an array by keys and values.
*
Expand Down

0 comments on commit 4029dbd

Please sign in to comment.