Skip to content

Commit

Permalink
Use native sort in PHP 8+
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderlee committed Jan 18, 2024
1 parent bb3a7d0 commit c5c6291
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PHP stable sort
===============
Version 2.0.4
Version 2.0.5
[![Build Status](https://travis-ci.org/vanderlee/PHP-stable-sort-functions.svg)](https://travis-ci.org/vanderlee/PHP-stable-sort-functions)

Copyright © 2015-2018 Martijn van der Lee (http://martijn.vanderlee.com).
Expand Down Expand Up @@ -45,6 +45,9 @@ counterparts.

Changes
-------
### 2.0.5
* PHP 8 sorts stable by default. Use native methods if PHP 8 or more recent.

### 2.0.1
* Merge @joelpittet 2.0.0 into main branch, cleaning Drupal specifics.

Expand Down
30 changes: 29 additions & 1 deletion classes/StableSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class StableSort

static public function arsort(array &$array, $sort_flags = SORT_REGULAR)
{
if (PHP_MAJOR_VERSION >= 8) {
return arsort($array, $sort_flags);
}

$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
Expand All @@ -35,7 +39,11 @@ static public function arsort(array &$array, $sort_flags = SORT_REGULAR)

static public function asort(array &$array, $sort_flags = SORT_REGULAR)
{
$index = 0;
if (PHP_MAJOR_VERSION >= 8) {
return asort($array, $sort_flags);
}

$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
}
Expand All @@ -60,6 +68,10 @@ static public function asort(array &$array, $sort_flags = SORT_REGULAR)

static public function natcasesort(array &$array)
{
if (PHP_MAJOR_VERSION >= 8) {
return natcasesort($array);
}

$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
Expand All @@ -79,6 +91,10 @@ static public function natcasesort(array &$array)

static public function natsort(array &$array)
{
if (PHP_MAJOR_VERSION >= 8) {
return natsort($array);
}

$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
Expand All @@ -98,6 +114,10 @@ static public function natsort(array &$array)

static public function uasort(array &$array, $value_compare_func)
{
if (PHP_MAJOR_VERSION >= 8) {
return uasort($array, $value_compare_func);
}

$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
Expand All @@ -117,6 +137,10 @@ static public function uasort(array &$array, $value_compare_func)

static public function uksort(array &$array, $value_compare_func)
{
if (PHP_MAJOR_VERSION >= 8) {
return uksort($array, $value_compare_func);
}

$keys = array_combine(array_keys($array), range(1, count($array)));

$result = uksort($array, function($a, $b) use($value_compare_func, $keys) {
Expand All @@ -129,6 +153,10 @@ static public function uksort(array &$array, $value_compare_func)

static public function usort(array &$array, $value_compare_func)
{
if (PHP_MAJOR_VERSION >= 8) {
return usort($array, $value_compare_func);
}

$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
Expand Down

0 comments on commit c5c6291

Please sign in to comment.