mirror of
https://github.com/processwire/processwire.git
synced 2025-08-17 12:10:45 +02:00
Add support for PHP SORT_* constants per processwire/processwire-issues#841
This commit is contained in:
@@ -82,6 +82,14 @@ class WireArray extends Wire implements \IteratorAggregate, \ArrayAccess, \Count
|
|||||||
*/
|
*/
|
||||||
protected $duplicateChecking = true;
|
protected $duplicateChecking = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flags for PHP sort functdions
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected $sortFlags = 0; // 0 == SORT_REGULAR
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct
|
* Construct
|
||||||
*
|
*
|
||||||
@@ -1257,10 +1265,15 @@ class WireArray extends Wire implements \IteratorAggregate, \ArrayAccess, \Count
|
|||||||
* #pw-group-manipulation
|
* #pw-group-manipulation
|
||||||
*
|
*
|
||||||
* @param string|array $properties Field names to sort by (CSV string or array).
|
* @param string|array $properties Field names to sort by (CSV string or array).
|
||||||
|
* @param int|null $flags Optionally specify sort flags (see sortFlags method for details).
|
||||||
* @return $this reference to current instance.
|
* @return $this reference to current instance.
|
||||||
*/
|
*/
|
||||||
public function sort($properties) {
|
public function sort($properties, $flags = null) {
|
||||||
return $this->_sort($properties);
|
$_flags = $this->sortFlags; // remember
|
||||||
|
if(is_int($flags)) $this->sortFlags($flags);
|
||||||
|
$result = $this->_sort($properties);
|
||||||
|
if(is_int($flags) && $flags !== $_flags) $this->sortFlags($_flags); // restore
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1294,6 +1307,32 @@ class WireArray extends Wire implements \IteratorAggregate, \ArrayAccess, \Count
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get or set sort flags that affect behavior of any sorting functions
|
||||||
|
*
|
||||||
|
* The following constants may be used when setting the sort flags:
|
||||||
|
*
|
||||||
|
* - `SORT_REGULAR` compare items normally (don’t change types)
|
||||||
|
* - `SORT_NUMERIC` compare items numerically
|
||||||
|
* - `SORT_STRING` compare items as strings
|
||||||
|
* - `SORT_LOCALE_STRING` compare items as strings, based on the current locale
|
||||||
|
* - `SORT_NATURAL` compare items as strings using “natural ordering” like natsort()
|
||||||
|
* - `SORT_FLAG_CASE` can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
|
||||||
|
*
|
||||||
|
* For more details, see `$sort_flags` argument at: https://www.php.net/manual/en/function.sort.php
|
||||||
|
*
|
||||||
|
* #pw-group-manipulation
|
||||||
|
*
|
||||||
|
* @param bool $sortFlags Optionally specify flag(s) to set
|
||||||
|
* @return int Returns current flags
|
||||||
|
* @since 3.0.129
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function sortFlags($sortFlags = false) {
|
||||||
|
if(is_int($sortFlags)) $this->sortFlags = $sortFlags;
|
||||||
|
return $this->sortFlags;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort given array by first given property.
|
* Sort given array by first given property.
|
||||||
*
|
*
|
||||||
@@ -1355,8 +1394,8 @@ class WireArray extends Wire implements \IteratorAggregate, \ArrayAccess, \Count
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sort the items by the keys we collected
|
// sort the items by the keys we collected
|
||||||
if($reverse) krsort($sortable);
|
if($reverse) krsort($sortable, $this->sortFlags);
|
||||||
else ksort($sortable);
|
else ksort($sortable, $this->sortFlags);
|
||||||
|
|
||||||
// add the items that resolved to no key to the end, as an array
|
// add the items that resolved to no key to the end, as an array
|
||||||
$sortable[] = $unidentified;
|
$sortable[] = $unidentified;
|
||||||
|
Reference in New Issue
Block a user