Sorting a list in woocommerce [duplicate] - php

How can I sort this array of objects by one of its fields, like name or count?
Array
(
[0] => stdClass Object
(
[ID] => 1
[name] => Mary Jane
[count] => 420
)
[1] => stdClass Object
(
[ID] => 2
[name] => Johnny
[count] => 234
)
[2] => stdClass Object
(
[ID] => 3
[name] => Kathy
[count] => 4354
)
....

Use usort, here's an example adapted from the manual:
function cmp($a, $b) {
return strcmp($a->name, $b->name);
}
usort($your_data, "cmp");
You can also use any callable as the second argument. Here are some examples:
Using anonymous functions (from PHP 5.3)
usort($your_data, function($a, $b) {return strcmp($a->name, $b->name);});
From inside a class
usort($your_data, array($this, "cmp")); // "cmp" should be a method in the class
Using arrow functions (from PHP 7.4)
usort($your_data, fn($a, $b) => strcmp($a->name, $b->name));
Also, if you're comparing numeric values, fn($a, $b) => $a->count - $b->count as the "compare" function should do the trick, or, if you want yet another way of doing the same thing, starting from PHP 7 you can use the Spaceship operator, like this: fn($a, $b) => $a->count <=> $b->count.

Heres a nicer way using closures
usort($your_data, function($a, $b)
{
return strcmp($a->name, $b->name);
});
Please note this is not in PHP's documentation but if you using 5.3+ closures are supported where callable arguments can be provided.

If you want to sort integer values:
// Desc sort
usort($array,function($first,$second){
return $first->number < $second->number;
});
// Asc sort
usort($array,function($first,$second){
return $first->number > $second->number;
});
UPDATED
with the string don't forget to convert to the same register (upper or lower)
// Desc sort
usort($array,function($first,$second){
return strtolower($first->text) < strtolower($second->text);
});
// Asc sort
usort($array,function($first,$second){
return strtolower($first->text) > strtolower($second->text);
});

if you're using php oop you might need to change to:
public static function cmp($a, $b)
{
return strcmp($a->name, $b->name);
}
//in this case FUNCTION_NAME would be cmp
usort($your_data, array('YOUR_CLASS_NAME','FUNCTION_NAME'));

usort($array, 'my_sort_function');
var_dump($array);
function my_sort_function($a, $b)
{
return $a->name < $b->name;
}
The same code will be with the count field.
More details about usort: http://ru2.php.net/usort
Btw, where did you get that array from? I hope that not from database?

If everything fails here is another solution:
$names = array();
foreach ($my_array as $my_object) {
$names[] = $my_object->name; //any object field
}
array_multisort($names, SORT_ASC, $my_array);
return $my_array;

You can use this function (works in PHP Version >= 5.3):
function sortArrayByKey(&$array,$key,$string = false,$asc = true){
if($string){
usort($array,function ($a, $b) use(&$key,&$asc)
{
if($asc) return strcmp(strtolower($a{$key}), strtolower($b{$key}));
else return strcmp(strtolower($b{$key}), strtolower($a{$key}));
});
}else{
usort($array,function ($a, $b) use(&$key,&$asc)
{
if($a[$key] == $b{$key}){return 0;}
if($asc) return ($a{$key} < $b{$key}) ? -1 : 1;
else return ($a{$key} > $b{$key}) ? -1 : 1;
});
}
}
Example:
sortArrayByKey($yourArray,"name",true); //String sort (ascending order)
sortArrayByKey($yourArray,"name",true,false); //String sort (descending order)
sortArrayByKey($yourArray,"id"); //number sort (ascending order)
sortArrayByKey($yourArray,"count",false,false); //number sort (descending order)

You can use usort, like this:
usort($array,function($first,$second){
return strcmp($first->name, $second->name);
});

if you want to sort dates
usort($threads,function($first,$second){
return strtotime($first->dateandtime) < strtotime($second->dateandtime);
});

To sort on one column of values, a combination of array_column() and array_multisort() is one sensible way. Demo
array_multisort(array_column($array, 'count'), $array);
Or only call upon usort() with the spaceship operator to perform less iterating in this scenario. Demo
usort($array, fn($a, $b) => $a->count <=> $b->count);
Notice that although the count values are cast as string type values in the input array, both sorting functions will correctly sort the values numerically instead of alphabetizing them (erroneously putting 23420 before 420). This is a reliable default feature.
Even if you are variably declaring the column to sort on, both approaches allow the variable to be used without any addition techniques.
Multisort Demo with variable
$property = 'count';
array_multisort(array_column($array, $property), $array);
Usort Demo with variable
$property = 'count';
usort($array, fn($a, $b) => $a->$property <=> $b->$property);
Both native sorting functions modify by reference, so do not try to access the sorted array by their return value.
array_multisort()'s default sorting direction is ascending, so it is of no benefit to explicitly use the SORT_ASC between the two array parameters. If descending sorting is desired, write SORT_DESC between the two arrays (as the second parameter).
usort() will sort ascending when the custom function body puts $a data on the left side of the spaceship operator and $b data on the right side. For sorting in a descending direction, just write $b data on the left and $a data on the right.
Both approaches are capable of receiving multiple sorting rules, but because this question only asks to sort on a single column, that guidance is inappropriate here.
It will be less efficient to call a function (like strcmp()) on every iteration while sorting. This is no longer best practice. Neither is using a two-way comparison (like > or <) to return a boolean outcome. A three-way comparison is expected from usort().
For sorting data with multiple rules/columns/properties, this answer gives good guidance.

Downside of all answers here is that they use static field names, so I wrote an adjusted version in OOP style. Assumed you are using getter methods you could directly use this Class and use the field name as parameter. Probably someone find it useful.
class CustomSort{
public $field = '';
public function cmp($a, $b)
{
/**
* field for order is in a class variable $field
* using getter function with naming convention getVariable() we set first letter to uppercase
* we use variable variable names - $a->{'varName'} would directly access a field
*/
return strcmp($a->{'get'.ucfirst($this->field)}(), $b->{'get'.ucfirst($this->field)}());
}
public function sortObjectArrayByField($array, $field)
{
$this->field = $field;
usort($array, array("Your\Namespace\CustomSort", "cmp"));;
return $array;
}
}

If you need local based string comparison, you can use strcoll instead of strcmp.
Remeber to first use setlocale with LC_COLLATE to set locale information if needed.
usort($your_data,function($a,$b){
setlocale (LC_COLLATE, 'pl_PL.UTF-8'); // Example of Polish language collation
return strcoll($a->name,$b->name);
});

A simple alternative that allows you to determine dynamically the field on which the sorting is based:
$order_by = 'name';
usort($your_data, function ($a, $b) use ($order_by)
{
return strcmp($a->{$order_by}, $b->{$order_by});
});
This is based on the Closure class, which allows anonymous functions. It is available since PHP 5.3.

If you are using this inside Codeigniter, you can use the methods:
usort($jobs, array($this->job_model, "sortJobs")); // function inside Model
usort($jobs, array($this, "sortJobs")); // Written inside Controller.
#rmooney thank you for the suggestion. It really helps me.

Thanks for the inspirations, I also had to add an external $translator parameter
usort($listable_products, function($a, $b) {
global $translator;
return strcmp($a->getFullTitle($translator), $b->getFullTitle($translator));
});

If you need to sort by only one field, then usort is a good choice. However, the solution quickly becomes messy if you need to sort by multiple fields. In this case, YaLinqo library* can be used, which implements SQL-like query syntax for arrays and objects. It has a pretty syntax for all cases:
$sortedByName = from($objects)->orderBy('$v->name');
$sortedByCount = from($objects)->orderBy('$v->count');
$sortedByCountAndName = from($objects)->orderBy('$v->count')->thenBy('$v->name');
Here, '$v->count' is a shorthand for function ($v) { return $v->count; } (either can be used). These method chains return iterators, but you can get arrays by adding ->toArray() in the end if you need it.
* developed by me

You can use sorted function from Nspl:
use function \nspl\a\sorted;
use function \nspl\op\propertyGetter;
use function \nspl\op\methodCaller;
// Sort by property value
$sortedByCount = sorted($objects, propertyGetter('count'));
// Or sort by result of method call
$sortedByName = sorted($objects, methodCaller('getName'));

This is what I have for a utility class
class Util
{
public static function sortArrayByName(&$arrayToSort, $meta) {
usort($arrayToSort, function($a, $b) use ($meta) {
return strcmp($a[$meta], $b[$meta]);
});
}
}
Call it:
Util::sortArrayByName($array, "array_property_name");

You can use usort like this
If you want to sort by number:
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
Or Abc char:
function cmp($a, $b)
{
return strcmp($a["fruit"], $b["fruit"]);
}
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");
See more: https://www.php.net/manual/en/function.usort.php

$array[0] = array('key_a' => 'z', 'key_b' => 'c');
$array[1] = array('key_a' => 'x', 'key_b' => 'b');
$array[2] = array('key_a' => 'y', 'key_b' => 'a');
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
usort($array, build_sorter('key_b'));

reference answer of Demodave to eating multi key
function array_sort_by(array $arr, $keys){
if(!is_array($keys))
$keyList = explode(',', $keys);
$keyList = array_keys(array_flip($keyList)); // array_unique
$keyList = array_reverse($keyList);
$result = &$arr;
foreach ($keyList as $key) {
if(array_key_exists($key, $arr))
$result = usort($result, function($a, $b) use ($key) { return strcmp($a->{$key}, $b->{$key}); });
}
return $result;
}

use this....
$array_list = [
"Apple" => 2,
"Pear" => 1,
"Orange" => 5,
"Lemon" => 1,
"Strawberry" => 2,
"Banana" => 3
];
function cmp($a, $b) {
return $b - $a;
}
$ao = new ArrayObject($object);
$ao->uasort('cmp');
print_r(json_encode($ao));
Bye!!!!

For my part, here is how I proceeded to sort an array of objects by object fields:
Code: (Demo) -- sorts by last_name ASC, then first_name ASC
<?php
$array = array(
(object)array(
'first_name' => 'Léa',
'last_name' => 'Weber',
),
(object)array(
'first_name' => 'Alexandre',
'last_name' => 'Dupont',
),
(object)array(
'first_name' => 'Léa',
'last_name' => 'Zotal',
),
(object)array(
'first_name' => 'Jérémie',
'last_name' => 'Hoffmann',
)
);
usort($array, function($a, $b) {
return [$a->last_name, $a->first_name]
<=>
[$b->last_name, $b->first_name];
});
var_export($array);
Outpout:
array (
0 =>
(object) array(
'first_name' => 'Alexandre',
'last_name' => 'Dupont',
),
1 =>
(object) array(
'first_name' => 'Jérémie',
'last_name' => 'Hoffmann',
),
2 =>
(object) array(
'first_name' => 'Léa',
'last_name' => 'Weber',
),
3 =>
(object) array(
'first_name' => 'Léa',
'last_name' => 'Zotal',
),
)
Arrow syntax with PHP7.4 and higher.
Makes sorting by multiple columns SUPER easy with the spaceship operator (<=>) aka the "Combined Comparison Operator" or "Three-way Comparison Operator".
Resource: https://wiki.php.net/rfc/combined-comparison-operator
https://stackoverflow.com/a/54647220/18090932

Related

sort array by object attributes [duplicate]

How can I sort this array of objects by one of its fields, like name or count?
Array
(
[0] => stdClass Object
(
[ID] => 1
[name] => Mary Jane
[count] => 420
)
[1] => stdClass Object
(
[ID] => 2
[name] => Johnny
[count] => 234
)
[2] => stdClass Object
(
[ID] => 3
[name] => Kathy
[count] => 4354
)
....
Use usort, here's an example adapted from the manual:
function cmp($a, $b) {
return strcmp($a->name, $b->name);
}
usort($your_data, "cmp");
You can also use any callable as the second argument. Here are some examples:
Using anonymous functions (from PHP 5.3)
usort($your_data, function($a, $b) {return strcmp($a->name, $b->name);});
From inside a class
usort($your_data, array($this, "cmp")); // "cmp" should be a method in the class
Using arrow functions (from PHP 7.4)
usort($your_data, fn($a, $b) => strcmp($a->name, $b->name));
Also, if you're comparing numeric values, fn($a, $b) => $a->count - $b->count as the "compare" function should do the trick, or, if you want yet another way of doing the same thing, starting from PHP 7 you can use the Spaceship operator, like this: fn($a, $b) => $a->count <=> $b->count.
Heres a nicer way using closures
usort($your_data, function($a, $b)
{
return strcmp($a->name, $b->name);
});
Please note this is not in PHP's documentation but if you using 5.3+ closures are supported where callable arguments can be provided.
If you want to sort integer values:
// Desc sort
usort($array,function($first,$second){
return $first->number < $second->number;
});
// Asc sort
usort($array,function($first,$second){
return $first->number > $second->number;
});
UPDATED
with the string don't forget to convert to the same register (upper or lower)
// Desc sort
usort($array,function($first,$second){
return strtolower($first->text) < strtolower($second->text);
});
// Asc sort
usort($array,function($first,$second){
return strtolower($first->text) > strtolower($second->text);
});
if you're using php oop you might need to change to:
public static function cmp($a, $b)
{
return strcmp($a->name, $b->name);
}
//in this case FUNCTION_NAME would be cmp
usort($your_data, array('YOUR_CLASS_NAME','FUNCTION_NAME'));
usort($array, 'my_sort_function');
var_dump($array);
function my_sort_function($a, $b)
{
return $a->name < $b->name;
}
The same code will be with the count field.
More details about usort: http://ru2.php.net/usort
Btw, where did you get that array from? I hope that not from database?
If everything fails here is another solution:
$names = array();
foreach ($my_array as $my_object) {
$names[] = $my_object->name; //any object field
}
array_multisort($names, SORT_ASC, $my_array);
return $my_array;
You can use this function (works in PHP Version >= 5.3):
function sortArrayByKey(&$array,$key,$string = false,$asc = true){
if($string){
usort($array,function ($a, $b) use(&$key,&$asc)
{
if($asc) return strcmp(strtolower($a{$key}), strtolower($b{$key}));
else return strcmp(strtolower($b{$key}), strtolower($a{$key}));
});
}else{
usort($array,function ($a, $b) use(&$key,&$asc)
{
if($a[$key] == $b{$key}){return 0;}
if($asc) return ($a{$key} < $b{$key}) ? -1 : 1;
else return ($a{$key} > $b{$key}) ? -1 : 1;
});
}
}
Example:
sortArrayByKey($yourArray,"name",true); //String sort (ascending order)
sortArrayByKey($yourArray,"name",true,false); //String sort (descending order)
sortArrayByKey($yourArray,"id"); //number sort (ascending order)
sortArrayByKey($yourArray,"count",false,false); //number sort (descending order)
You can use usort, like this:
usort($array,function($first,$second){
return strcmp($first->name, $second->name);
});
if you want to sort dates
usort($threads,function($first,$second){
return strtotime($first->dateandtime) < strtotime($second->dateandtime);
});
To sort on one column of values, a combination of array_column() and array_multisort() is one sensible way. Demo
array_multisort(array_column($array, 'count'), $array);
Or only call upon usort() with the spaceship operator to perform less iterating in this scenario. Demo
usort($array, fn($a, $b) => $a->count <=> $b->count);
Notice that although the count values are cast as string type values in the input array, both sorting functions will correctly sort the values numerically instead of alphabetizing them (erroneously putting 23420 before 420). This is a reliable default feature.
Even if you are variably declaring the column to sort on, both approaches allow the variable to be used without any addition techniques.
Multisort Demo with variable
$property = 'count';
array_multisort(array_column($array, $property), $array);
Usort Demo with variable
$property = 'count';
usort($array, fn($a, $b) => $a->$property <=> $b->$property);
Both native sorting functions modify by reference, so do not try to access the sorted array by their return value.
array_multisort()'s default sorting direction is ascending, so it is of no benefit to explicitly use the SORT_ASC between the two array parameters. If descending sorting is desired, write SORT_DESC between the two arrays (as the second parameter).
usort() will sort ascending when the custom function body puts $a data on the left side of the spaceship operator and $b data on the right side. For sorting in a descending direction, just write $b data on the left and $a data on the right.
Both approaches are capable of receiving multiple sorting rules, but because this question only asks to sort on a single column, that guidance is inappropriate here.
It will be less efficient to call a function (like strcmp()) on every iteration while sorting. This is no longer best practice. Neither is using a two-way comparison (like > or <) to return a boolean outcome. A three-way comparison is expected from usort().
For sorting data with multiple rules/columns/properties, this answer gives good guidance.
Downside of all answers here is that they use static field names, so I wrote an adjusted version in OOP style. Assumed you are using getter methods you could directly use this Class and use the field name as parameter. Probably someone find it useful.
class CustomSort{
public $field = '';
public function cmp($a, $b)
{
/**
* field for order is in a class variable $field
* using getter function with naming convention getVariable() we set first letter to uppercase
* we use variable variable names - $a->{'varName'} would directly access a field
*/
return strcmp($a->{'get'.ucfirst($this->field)}(), $b->{'get'.ucfirst($this->field)}());
}
public function sortObjectArrayByField($array, $field)
{
$this->field = $field;
usort($array, array("Your\Namespace\CustomSort", "cmp"));;
return $array;
}
}
If you need local based string comparison, you can use strcoll instead of strcmp.
Remeber to first use setlocale with LC_COLLATE to set locale information if needed.
usort($your_data,function($a,$b){
setlocale (LC_COLLATE, 'pl_PL.UTF-8'); // Example of Polish language collation
return strcoll($a->name,$b->name);
});
A simple alternative that allows you to determine dynamically the field on which the sorting is based:
$order_by = 'name';
usort($your_data, function ($a, $b) use ($order_by)
{
return strcmp($a->{$order_by}, $b->{$order_by});
});
This is based on the Closure class, which allows anonymous functions. It is available since PHP 5.3.
If you are using this inside Codeigniter, you can use the methods:
usort($jobs, array($this->job_model, "sortJobs")); // function inside Model
usort($jobs, array($this, "sortJobs")); // Written inside Controller.
#rmooney thank you for the suggestion. It really helps me.
Thanks for the inspirations, I also had to add an external $translator parameter
usort($listable_products, function($a, $b) {
global $translator;
return strcmp($a->getFullTitle($translator), $b->getFullTitle($translator));
});
If you need to sort by only one field, then usort is a good choice. However, the solution quickly becomes messy if you need to sort by multiple fields. In this case, YaLinqo library* can be used, which implements SQL-like query syntax for arrays and objects. It has a pretty syntax for all cases:
$sortedByName = from($objects)->orderBy('$v->name');
$sortedByCount = from($objects)->orderBy('$v->count');
$sortedByCountAndName = from($objects)->orderBy('$v->count')->thenBy('$v->name');
Here, '$v->count' is a shorthand for function ($v) { return $v->count; } (either can be used). These method chains return iterators, but you can get arrays by adding ->toArray() in the end if you need it.
* developed by me
You can use sorted function from Nspl:
use function \nspl\a\sorted;
use function \nspl\op\propertyGetter;
use function \nspl\op\methodCaller;
// Sort by property value
$sortedByCount = sorted($objects, propertyGetter('count'));
// Or sort by result of method call
$sortedByName = sorted($objects, methodCaller('getName'));
This is what I have for a utility class
class Util
{
public static function sortArrayByName(&$arrayToSort, $meta) {
usort($arrayToSort, function($a, $b) use ($meta) {
return strcmp($a[$meta], $b[$meta]);
});
}
}
Call it:
Util::sortArrayByName($array, "array_property_name");
You can use usort like this
If you want to sort by number:
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
Or Abc char:
function cmp($a, $b)
{
return strcmp($a["fruit"], $b["fruit"]);
}
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");
See more: https://www.php.net/manual/en/function.usort.php
$array[0] = array('key_a' => 'z', 'key_b' => 'c');
$array[1] = array('key_a' => 'x', 'key_b' => 'b');
$array[2] = array('key_a' => 'y', 'key_b' => 'a');
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
usort($array, build_sorter('key_b'));
reference answer of Demodave to eating multi key
function array_sort_by(array $arr, $keys){
if(!is_array($keys))
$keyList = explode(',', $keys);
$keyList = array_keys(array_flip($keyList)); // array_unique
$keyList = array_reverse($keyList);
$result = &$arr;
foreach ($keyList as $key) {
if(array_key_exists($key, $arr))
$result = usort($result, function($a, $b) use ($key) { return strcmp($a->{$key}, $b->{$key}); });
}
return $result;
}
use this....
$array_list = [
"Apple" => 2,
"Pear" => 1,
"Orange" => 5,
"Lemon" => 1,
"Strawberry" => 2,
"Banana" => 3
];
function cmp($a, $b) {
return $b - $a;
}
$ao = new ArrayObject($object);
$ao->uasort('cmp');
print_r(json_encode($ao));
Bye!!!!
For my part, here is how I proceeded to sort an array of objects by object fields:
Code: (Demo) -- sorts by last_name ASC, then first_name ASC
<?php
$array = array(
(object)array(
'first_name' => 'Léa',
'last_name' => 'Weber',
),
(object)array(
'first_name' => 'Alexandre',
'last_name' => 'Dupont',
),
(object)array(
'first_name' => 'Léa',
'last_name' => 'Zotal',
),
(object)array(
'first_name' => 'Jérémie',
'last_name' => 'Hoffmann',
)
);
usort($array, function($a, $b) {
return [$a->last_name, $a->first_name]
<=>
[$b->last_name, $b->first_name];
});
var_export($array);
Outpout:
array (
0 =>
(object) array(
'first_name' => 'Alexandre',
'last_name' => 'Dupont',
),
1 =>
(object) array(
'first_name' => 'Jérémie',
'last_name' => 'Hoffmann',
),
2 =>
(object) array(
'first_name' => 'Léa',
'last_name' => 'Weber',
),
3 =>
(object) array(
'first_name' => 'Léa',
'last_name' => 'Zotal',
),
)
Arrow syntax with PHP7.4 and higher.
Makes sorting by multiple columns SUPER easy with the spaceship operator (<=>) aka the "Combined Comparison Operator" or "Three-way Comparison Operator".
Resource: https://wiki.php.net/rfc/combined-comparison-operator
https://stackoverflow.com/a/54647220/18090932

How to sort array using json key value using PHP [duplicate]

How can I sort this array by the value of the "order" key?
Even though the values are currently sequential, they will not always be.
Array
(
[0] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
[2] => Array
(
[hashtag] => e7d31fc0602fb2ede144d18cdffd816b
[title] => Ready
[order] => 1
)
)
Try a usort. If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
function sortByOrder($a, $b) {
return $a['order'] - $b['order'];
}
usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) {
return $a['order'] - $b['order'];
});
With PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});
Finally, in PHP 7.4 you can clean up a bit with an arrow function:
usort($myArray, fn($a, $b) => $a['order'] <=> $b['order']);
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) {
$retval = $a['order'] <=> $b['order'];
if ($retval == 0) {
$retval = $a['suborder'] <=> $b['suborder'];
if ($retval == 0) {
$retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
}
}
return $retval;
});
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual.
function aasort (&$array, $key) {
$sorter = array();
$ret = array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii] = $va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii] = $array[$ii];
}
$array = $ret;
}
aasort($your_array, "order");
I use this function:
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key => $row) {
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
array_sort_by_column($array, 'order');
Edit
This answer is at least ten years old, and there are likely better solutions now, but I am adding some extra info as requested in a couple of comments.
It works because array_multisort() can sort multiple arrays. Example input:
Array
(
[0] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
First $sort_col is made which is an two dimensional array with the values being what we want to sort by and the keys matching the input array. For example for this input, choosing key $sort_col "order" we get:
Array
(
[0] => 3,
[1] => 2
)
array_multisort() then sorts that array (resulting in key order 1, 0) but this is only the two dimensional array. So the original input array is also passed as the $rest argument. As the keys match it will be sorted so its keys are also in the same order, giving the desired result.
Note:
it is passed by reference so that the supplied array is modified in place.
array_multisort() can sort multiple additional array like this, not just one
To achieve this we can use "array_multisort" method which 'Sorts multiple or multi-dimensional arrays'. It's method parameters are
$keys - an array being sorted
SORT_ASC - sort order (ascending)
sort flags (compare items normally(don't change types)
or numerically or as strings)
$new - then rest of the arrays. Only elements
corresponding to equivalent elements in previous
arrays are compared.
'sort flags' is SORT_REGULAR by default and it is omitted.
$new = [
[
'hashtag' => 'a7e87329b5eab8578f4f1098a152d6f4',
'title' => 'Flower',
'order' => 3,
],
[
'hashtag' => 'b24ce0cd392a5b0b8dedc66c25213594',
'title' => 'Free',
'order' => 2,
],
[
'hashtag' => 'e7d31fc0602fb2ede144d18cdffd816b',
'title' => 'Ready',
'order' => 1,
],
];
$keys = array_column($new, 'order');
array_multisort($keys, SORT_ASC, $new);
var_dump($new);
Result:
Array
(
[0] => Array
(
[hashtag] => e7d31fc0602fb2ede144d18cdffd816b
[title] => Ready
[order] => 1
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
[2] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
)
I usually use usort, and pass my own comparison function. In this case, it is very simple:
function compareOrder($a, $b)
{
return $a['order'] - $b['order'];
}
usort($array, 'compareOrder');
In PHP 7 using the spaceship operator:
usort($array, function($a, $b) {
return $a['order'] <=> $b['order'];
});
To sort the array by the value of the "title" key, use:
uasort($myArray, function($a, $b) {
return strcmp($a['title'], $b['title']);
});
strcmp compare the strings.
uasort() maintains the array keys as they were defined.
Use array_multisort(), array_map()
array_multisort(array_map(function($element) {
return $element['order'];
}, $array), SORT_ASC, $array);
print_r($array);
DEMO
$sort = array();
$array_lowercase = array_map('strtolower', $array_to_be_sorted);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $alphabetically_ordered_array);
This takes care of both upper and lower case alphabets.
As the accepted answer states you can use:
usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});
If you need sort by more than one column, then you would do the following:
usort($myArray, function($a, $b) {
return [$a['column1'],$a['column2']] <=> [$b['column1'],$b['column2']];
});
This can be extended to any number of columns in your data. This relies on the fact you can directly compare arrays in PHP. In the above example the array would be sorted first by column1 and then by column2. But you can sort by the columns in any order e.g.:
usort($myArray, function($a, $b) {
return [$a['column2'],$a['column1']] <=> [$b['column2'],$b['column1']];
});
If you need to sort one column ascending and another descending, then swap the descending column to the other side of the operator <=>:
usort($myArray, function($a, $b) {
return [$a['column1'],$b['column2']] <=> [$b['column1'],$a['column2']];
});
The most flexible approach would be using this method:
Arr::sortByKeys(array $array, $keys, bool $assoc = true): array
Here's why:
You can sort by any key (also nested like 'key1.key2.key3' or ['k1', 'k2', 'k3'])
It works both on associative and not associative arrays ($assoc flag)
It doesn't use references - it returns a new sorted array
In your case it would be as simple as:
$sortedArray = Arr::sortByKeys($array, 'order');
This method is a part of this library.
The working "arrow function" syntax with PHP 7.4 and above:
uasort($yourArray, fn($a, $b) => $a['order'] <=> $b['order']);
pretty print
echo '<pre>';
print_r($yourArray);
If anyone needs sort according to a key, the best is to use the below:
usort($array, build_sorter('order'));
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
This solution is for usort() with an easy-to-remember notation for multidimensional sorting. The spaceship operator <=> is used, which is available from PHP 7.
usort($in,function($a,$b){
return $a['first'] <=> $b['first'] //first asc
?: $a['second'] <=> $b['second'] //second asc
?: $b['third'] <=> $a['third'] //third desc (a b swapped!)
//etc
;
});
Examples:
$in = [
['firstname' => 'Anton', 'surname' => 'Gruber', 'birthdate' => '03.08.1967', 'rank' => 3],
['firstname' => 'Anna', 'surname' => 'Egger', 'birthdate' => '04.01.1960', 'rank' => 1],
['firstname' => 'Paul', 'surname' => 'Mueller', 'birthdate' => '15.10.1971', 'rank' => 2],
['firstname' => 'Marie', 'surname' => 'Schmidt ', 'birthdate' => '24.12.1963', 'rank' => 2],
['firstname' => 'Emma', 'surname' => 'Mueller', 'birthdate' => '23.11.1969', 'rank' => 2],
];
First task: Order By rank asc, surname asc
usort($in,function($a,$b){
return $a['rank'] <=> $b['rank'] //first asc
?: $a['surname'] <=> $b['surname'] //second asc
;
});
Second task: Order By rank desc, surname asc, firstmame asc
usort($in,function($a,$b){
return $b['rank'] <=> $a['rank'] //first desc
?: $a['surname'] <=> $b['surname'] //second asc
?: $a['firstname'] <=> $b['firstname'] //third asc
;
});
Third task: Order By rank desc, birthdate asc
The date cannot be sorted in this notation. It is converted with strtotime.
usort($in,function($a,$b){
return $b['rank'] <=> $a['rank'] //first desc
?: strtotime($a['birthdate']) <=> strtotime($b['birthdate']) //second asc
;
});
You could use usort and a user-defined sort function with a callback function:
usort($new, fn($a, $b) => $a['order'] - $b['order']);
TRICK: you could use a > b or a - b or a <=> b for sorting in an ascending order. For a descending order just the swap position of a and b.
example with class:
class user
{
private $key;
public function __construct(string $key)
{
$this->key = $key;
}
public function __invoke($a, $b)
{
return $a[$this->key] <=> $b[$this->key];
}
}
$user = [
['id' => 1, 'name' => 'Oliver', 'id_card' => 4444],
['id' => 3, 'name' => 'Jack', 'id_card' => 5555],
['id' => 2, 'name' => 'Harry', 'id_card' => 6666]
];
// sort user by id
usort($user, new user('id'));
var_dump($user);
Let's face it: PHP does not have a simple out-of-the box function to properly handle every array sort scenario.
This routine is intuitive, which means faster debugging and maintenance:
// Automatic population of the array
$tempArray = array();
$annotations = array();
// ... some code
// SQL $sql retrieves result array $result
// $row[0] is the ID, but is populated out of order (comes from
// multiple selects populating various dimensions for the same DATE
// for example
while($row = mysql_fetch_array($result)) {
$needle = $row[0];
arrayIndexes($needle); // Create a parallel array with IDs only
$annotations[$needle]['someDimension'] = $row[1]; // Whatever
}
asort($tempArray);
foreach ($tempArray as $arrayKey) {
$dataInOrder = $annotations[$arrayKey]['someDimension'];
// .... more code
}
function arrayIndexes ($needle) {
global $tempArray;
if (!in_array($needle, $tempArray)) {
array_push($tempArray, $needle);
}
}

Sort a PHP array by a rigid order template [duplicate]

How can I sort this array by the value of the "order" key?
Even though the values are currently sequential, they will not always be.
Array
(
[0] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
[2] => Array
(
[hashtag] => e7d31fc0602fb2ede144d18cdffd816b
[title] => Ready
[order] => 1
)
)
Try a usort. If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
function sortByOrder($a, $b) {
return $a['order'] - $b['order'];
}
usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) {
return $a['order'] - $b['order'];
});
With PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});
Finally, in PHP 7.4 you can clean up a bit with an arrow function:
usort($myArray, fn($a, $b) => $a['order'] <=> $b['order']);
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) {
$retval = $a['order'] <=> $b['order'];
if ($retval == 0) {
$retval = $a['suborder'] <=> $b['suborder'];
if ($retval == 0) {
$retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
}
}
return $retval;
});
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual.
function aasort (&$array, $key) {
$sorter = array();
$ret = array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii] = $va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii] = $array[$ii];
}
$array = $ret;
}
aasort($your_array, "order");
I use this function:
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key => $row) {
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
array_sort_by_column($array, 'order');
Edit
This answer is at least ten years old, and there are likely better solutions now, but I am adding some extra info as requested in a couple of comments.
It works because array_multisort() can sort multiple arrays. Example input:
Array
(
[0] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
First $sort_col is made which is an two dimensional array with the values being what we want to sort by and the keys matching the input array. For example for this input, choosing key $sort_col "order" we get:
Array
(
[0] => 3,
[1] => 2
)
array_multisort() then sorts that array (resulting in key order 1, 0) but this is only the two dimensional array. So the original input array is also passed as the $rest argument. As the keys match it will be sorted so its keys are also in the same order, giving the desired result.
Note:
it is passed by reference so that the supplied array is modified in place.
array_multisort() can sort multiple additional array like this, not just one
To achieve this we can use "array_multisort" method which 'Sorts multiple or multi-dimensional arrays'. It's method parameters are
$keys - an array being sorted
SORT_ASC - sort order (ascending)
sort flags (compare items normally(don't change types)
or numerically or as strings)
$new - then rest of the arrays. Only elements
corresponding to equivalent elements in previous
arrays are compared.
'sort flags' is SORT_REGULAR by default and it is omitted.
$new = [
[
'hashtag' => 'a7e87329b5eab8578f4f1098a152d6f4',
'title' => 'Flower',
'order' => 3,
],
[
'hashtag' => 'b24ce0cd392a5b0b8dedc66c25213594',
'title' => 'Free',
'order' => 2,
],
[
'hashtag' => 'e7d31fc0602fb2ede144d18cdffd816b',
'title' => 'Ready',
'order' => 1,
],
];
$keys = array_column($new, 'order');
array_multisort($keys, SORT_ASC, $new);
var_dump($new);
Result:
Array
(
[0] => Array
(
[hashtag] => e7d31fc0602fb2ede144d18cdffd816b
[title] => Ready
[order] => 1
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
[2] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
)
I usually use usort, and pass my own comparison function. In this case, it is very simple:
function compareOrder($a, $b)
{
return $a['order'] - $b['order'];
}
usort($array, 'compareOrder');
In PHP 7 using the spaceship operator:
usort($array, function($a, $b) {
return $a['order'] <=> $b['order'];
});
To sort the array by the value of the "title" key, use:
uasort($myArray, function($a, $b) {
return strcmp($a['title'], $b['title']);
});
strcmp compare the strings.
uasort() maintains the array keys as they were defined.
Use array_multisort(), array_map()
array_multisort(array_map(function($element) {
return $element['order'];
}, $array), SORT_ASC, $array);
print_r($array);
DEMO
$sort = array();
$array_lowercase = array_map('strtolower', $array_to_be_sorted);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $alphabetically_ordered_array);
This takes care of both upper and lower case alphabets.
The most flexible approach would be using this method:
Arr::sortByKeys(array $array, $keys, bool $assoc = true): array
Here's why:
You can sort by any key (also nested like 'key1.key2.key3' or ['k1', 'k2', 'k3'])
It works both on associative and not associative arrays ($assoc flag)
It doesn't use references - it returns a new sorted array
In your case it would be as simple as:
$sortedArray = Arr::sortByKeys($array, 'order');
This method is a part of this library.
The working "arrow function" syntax with PHP 7.4 and above:
uasort($yourArray, fn($a, $b) => $a['order'] <=> $b['order']);
pretty print
echo '<pre>';
print_r($yourArray);
As the accepted answer states you can use:
usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});
If you need sort by more than one column, then you would do the following:
usort($myArray, function($a, $b) {
return [$a['column1'],$a['column2']] <=> [$b['column1'],$b['column2']];
});
This can be extended to any number of columns in your data. This relies on the fact you can directly compare arrays in PHP. In the above example the array would be sorted first by column1 and then by column2. But you can sort by the columns in any order e.g.:
usort($myArray, function($a, $b) {
return [$a['column2'],$a['column1']] <=> [$b['column2'],$b['column1']];
});
If you need to sort one column ascending and another descending, then swap the descending column to the other side of the operator <=>:
usort($myArray, function($a, $b) {
return [$a['column1'],$b['column2']] <=> [$b['column1'],$a['column2']];
});
If anyone needs sort according to a key, the best is to use the below:
usort($array, build_sorter('order'));
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
This solution is for usort() with an easy-to-remember notation for multidimensional sorting. The spaceship operator <=> is used, which is available from PHP 7.
usort($in,function($a,$b){
return $a['first'] <=> $b['first'] //first asc
?: $a['second'] <=> $b['second'] //second asc
?: $b['third'] <=> $a['third'] //third desc (a b swapped!)
//etc
;
});
Examples:
$in = [
['firstname' => 'Anton', 'surname' => 'Gruber', 'birthdate' => '03.08.1967', 'rank' => 3],
['firstname' => 'Anna', 'surname' => 'Egger', 'birthdate' => '04.01.1960', 'rank' => 1],
['firstname' => 'Paul', 'surname' => 'Mueller', 'birthdate' => '15.10.1971', 'rank' => 2],
['firstname' => 'Marie', 'surname' => 'Schmidt ', 'birthdate' => '24.12.1963', 'rank' => 2],
['firstname' => 'Emma', 'surname' => 'Mueller', 'birthdate' => '23.11.1969', 'rank' => 2],
];
First task: Order By rank asc, surname asc
usort($in,function($a,$b){
return $a['rank'] <=> $b['rank'] //first asc
?: $a['surname'] <=> $b['surname'] //second asc
;
});
Second task: Order By rank desc, surname asc, firstmame asc
usort($in,function($a,$b){
return $b['rank'] <=> $a['rank'] //first desc
?: $a['surname'] <=> $b['surname'] //second asc
?: $a['firstname'] <=> $b['firstname'] //third asc
;
});
Third task: Order By rank desc, birthdate asc
The date cannot be sorted in this notation. It is converted with strtotime.
usort($in,function($a,$b){
return $b['rank'] <=> $a['rank'] //first desc
?: strtotime($a['birthdate']) <=> strtotime($b['birthdate']) //second asc
;
});
You could use usort and a user-defined sort function with a callback function:
usort($new, fn($a, $b) => $a['order'] - $b['order']);
TRICK: you could use a > b or a - b or a <=> b for sorting in an ascending order. For a descending order just the swap position of a and b.
example with class:
class user
{
private $key;
public function __construct(string $key)
{
$this->key = $key;
}
public function __invoke($a, $b)
{
return $a[$this->key] <=> $b[$this->key];
}
}
$user = [
['id' => 1, 'name' => 'Oliver', 'id_card' => 4444],
['id' => 3, 'name' => 'Jack', 'id_card' => 5555],
['id' => 2, 'name' => 'Harry', 'id_card' => 6666]
];
// sort user by id
usort($user, new user('id'));
var_dump($user);
Let's face it: PHP does not have a simple out-of-the box function to properly handle every array sort scenario.
This routine is intuitive, which means faster debugging and maintenance:
// Automatic population of the array
$tempArray = array();
$annotations = array();
// ... some code
// SQL $sql retrieves result array $result
// $row[0] is the ID, but is populated out of order (comes from
// multiple selects populating various dimensions for the same DATE
// for example
while($row = mysql_fetch_array($result)) {
$needle = $row[0];
arrayIndexes($needle); // Create a parallel array with IDs only
$annotations[$needle]['someDimension'] = $row[1]; // Whatever
}
asort($tempArray);
foreach ($tempArray as $arrayKey) {
$dataInOrder = $annotations[$arrayKey]['someDimension'];
// .... more code
}
function arrayIndexes ($needle) {
global $tempArray;
if (!in_array($needle, $tempArray)) {
array_push($tempArray, $needle);
}
}

How to perform a natural sort in php using usort

Does anyone know what the function is to perform a natural order sort using the usort function in PHP on an object.
Lets say the object ($obj->Rate)has a range of values in
$obj->10
$obj->1
$obj->2
$obj->20
$obj->22
What is I am trying to get the sort function to return
$obj->22
$obj->20
$obj->10
$obj->2
$obj->1
As my current standard sort function
function MySort($a, $b)
{
if ($a->Rate == $b->Rate)
{
return 0;
}
return ($a->Rate < $b->Rate) ? -1 : 1;
}
is returning
$obj->1
$obj->10
$obj->2
$obj->20
$obj->22
Use strnatcmp for your comparison function. e.g. it's as simple as
function mysort($a, $b) {
return strnatcmp($a->rate, $b->rate);
}
There are a few ways to sort by your Rate properties in a numeric and descending.
Here are some demonstrations based on your input:
$objects = [
(object)['Rate' => '10'],
(object)['Rate' => '1'],
(object)['Rate' => '2'],
(object)['Rate' => '20'],
(object)['Rate' => '22']
];
array_multisort() is clear and expressive: (Demo)
array_multisort(array_column($objects, 'Rate'), SORT_DESC, SORT_NUMERIC, $objects);
usort(): (Demo)
usort($objects, function($a, $b) {
return $b->Rate <=> $a->Rate;
});
usort() with arrow function syntax from PHP7.4: (Demo)
usort($objects, fn($a, $b) => $b->Rate <=> $a->Rate);
PHP's spaceship operator (<=>) will automatically evaluate two numeric strings as numbers -- no extra/iterated function calls or flags are necessary.

Sort array of objects by one property

How can I sort this array of objects by one of its fields, like name or count?
Array
(
[0] => stdClass Object
(
[ID] => 1
[name] => Mary Jane
[count] => 420
)
[1] => stdClass Object
(
[ID] => 2
[name] => Johnny
[count] => 234
)
[2] => stdClass Object
(
[ID] => 3
[name] => Kathy
[count] => 4354
)
....
Use usort, here's an example adapted from the manual:
function cmp($a, $b) {
return strcmp($a->name, $b->name);
}
usort($your_data, "cmp");
You can also use any callable as the second argument. Here are some examples:
Using anonymous functions (from PHP 5.3)
usort($your_data, function($a, $b) {return strcmp($a->name, $b->name);});
From inside a class
usort($your_data, array($this, "cmp")); // "cmp" should be a method in the class
Using arrow functions (from PHP 7.4)
usort($your_data, fn($a, $b) => strcmp($a->name, $b->name));
Also, if you're comparing numeric values, fn($a, $b) => $a->count - $b->count as the "compare" function should do the trick, or, if you want yet another way of doing the same thing, starting from PHP 7 you can use the Spaceship operator, like this: fn($a, $b) => $a->count <=> $b->count.
Heres a nicer way using closures
usort($your_data, function($a, $b)
{
return strcmp($a->name, $b->name);
});
Please note this is not in PHP's documentation but if you using 5.3+ closures are supported where callable arguments can be provided.
If you want to sort integer values:
// Desc sort
usort($array,function($first,$second){
return $first->number < $second->number;
});
// Asc sort
usort($array,function($first,$second){
return $first->number > $second->number;
});
UPDATED
with the string don't forget to convert to the same register (upper or lower)
// Desc sort
usort($array,function($first,$second){
return strtolower($first->text) < strtolower($second->text);
});
// Asc sort
usort($array,function($first,$second){
return strtolower($first->text) > strtolower($second->text);
});
if you're using php oop you might need to change to:
public static function cmp($a, $b)
{
return strcmp($a->name, $b->name);
}
//in this case FUNCTION_NAME would be cmp
usort($your_data, array('YOUR_CLASS_NAME','FUNCTION_NAME'));
usort($array, 'my_sort_function');
var_dump($array);
function my_sort_function($a, $b)
{
return $a->name < $b->name;
}
The same code will be with the count field.
More details about usort: http://ru2.php.net/usort
Btw, where did you get that array from? I hope that not from database?
If everything fails here is another solution:
$names = array();
foreach ($my_array as $my_object) {
$names[] = $my_object->name; //any object field
}
array_multisort($names, SORT_ASC, $my_array);
return $my_array;
You can use this function (works in PHP Version >= 5.3):
function sortArrayByKey(&$array,$key,$string = false,$asc = true){
if($string){
usort($array,function ($a, $b) use(&$key,&$asc)
{
if($asc) return strcmp(strtolower($a{$key}), strtolower($b{$key}));
else return strcmp(strtolower($b{$key}), strtolower($a{$key}));
});
}else{
usort($array,function ($a, $b) use(&$key,&$asc)
{
if($a[$key] == $b{$key}){return 0;}
if($asc) return ($a{$key} < $b{$key}) ? -1 : 1;
else return ($a{$key} > $b{$key}) ? -1 : 1;
});
}
}
Example:
sortArrayByKey($yourArray,"name",true); //String sort (ascending order)
sortArrayByKey($yourArray,"name",true,false); //String sort (descending order)
sortArrayByKey($yourArray,"id"); //number sort (ascending order)
sortArrayByKey($yourArray,"count",false,false); //number sort (descending order)
You can use usort, like this:
usort($array,function($first,$second){
return strcmp($first->name, $second->name);
});
if you want to sort dates
usort($threads,function($first,$second){
return strtotime($first->dateandtime) < strtotime($second->dateandtime);
});
To sort on one column of values, a combination of array_column() and array_multisort() is one sensible way. Demo
array_multisort(array_column($array, 'count'), $array);
Or only call upon usort() with the spaceship operator to perform less iterating in this scenario. Demo
usort($array, fn($a, $b) => $a->count <=> $b->count);
Notice that although the count values are cast as string type values in the input array, both sorting functions will correctly sort the values numerically instead of alphabetizing them (erroneously putting 23420 before 420). This is a reliable default feature.
Even if you are variably declaring the column to sort on, both approaches allow the variable to be used without any addition techniques.
Multisort Demo with variable
$property = 'count';
array_multisort(array_column($array, $property), $array);
Usort Demo with variable
$property = 'count';
usort($array, fn($a, $b) => $a->$property <=> $b->$property);
Both native sorting functions modify by reference, so do not try to access the sorted array by their return value.
array_multisort()'s default sorting direction is ascending, so it is of no benefit to explicitly use the SORT_ASC between the two array parameters. If descending sorting is desired, write SORT_DESC between the two arrays (as the second parameter).
usort() will sort ascending when the custom function body puts $a data on the left side of the spaceship operator and $b data on the right side. For sorting in a descending direction, just write $b data on the left and $a data on the right.
Both approaches are capable of receiving multiple sorting rules, but because this question only asks to sort on a single column, that guidance is inappropriate here.
It will be less efficient to call a function (like strcmp()) on every iteration while sorting. This is no longer best practice. Neither is using a two-way comparison (like > or <) to return a boolean outcome. A three-way comparison is expected from usort().
For sorting data with multiple rules/columns/properties, this answer gives good guidance.
Downside of all answers here is that they use static field names, so I wrote an adjusted version in OOP style. Assumed you are using getter methods you could directly use this Class and use the field name as parameter. Probably someone find it useful.
class CustomSort{
public $field = '';
public function cmp($a, $b)
{
/**
* field for order is in a class variable $field
* using getter function with naming convention getVariable() we set first letter to uppercase
* we use variable variable names - $a->{'varName'} would directly access a field
*/
return strcmp($a->{'get'.ucfirst($this->field)}(), $b->{'get'.ucfirst($this->field)}());
}
public function sortObjectArrayByField($array, $field)
{
$this->field = $field;
usort($array, array("Your\Namespace\CustomSort", "cmp"));;
return $array;
}
}
If you need local based string comparison, you can use strcoll instead of strcmp.
Remeber to first use setlocale with LC_COLLATE to set locale information if needed.
usort($your_data,function($a,$b){
setlocale (LC_COLLATE, 'pl_PL.UTF-8'); // Example of Polish language collation
return strcoll($a->name,$b->name);
});
A simple alternative that allows you to determine dynamically the field on which the sorting is based:
$order_by = 'name';
usort($your_data, function ($a, $b) use ($order_by)
{
return strcmp($a->{$order_by}, $b->{$order_by});
});
This is based on the Closure class, which allows anonymous functions. It is available since PHP 5.3.
If you are using this inside Codeigniter, you can use the methods:
usort($jobs, array($this->job_model, "sortJobs")); // function inside Model
usort($jobs, array($this, "sortJobs")); // Written inside Controller.
#rmooney thank you for the suggestion. It really helps me.
Thanks for the inspirations, I also had to add an external $translator parameter
usort($listable_products, function($a, $b) {
global $translator;
return strcmp($a->getFullTitle($translator), $b->getFullTitle($translator));
});
If you need to sort by only one field, then usort is a good choice. However, the solution quickly becomes messy if you need to sort by multiple fields. In this case, YaLinqo library* can be used, which implements SQL-like query syntax for arrays and objects. It has a pretty syntax for all cases:
$sortedByName = from($objects)->orderBy('$v->name');
$sortedByCount = from($objects)->orderBy('$v->count');
$sortedByCountAndName = from($objects)->orderBy('$v->count')->thenBy('$v->name');
Here, '$v->count' is a shorthand for function ($v) { return $v->count; } (either can be used). These method chains return iterators, but you can get arrays by adding ->toArray() in the end if you need it.
* developed by me
You can use sorted function from Nspl:
use function \nspl\a\sorted;
use function \nspl\op\propertyGetter;
use function \nspl\op\methodCaller;
// Sort by property value
$sortedByCount = sorted($objects, propertyGetter('count'));
// Or sort by result of method call
$sortedByName = sorted($objects, methodCaller('getName'));
This is what I have for a utility class
class Util
{
public static function sortArrayByName(&$arrayToSort, $meta) {
usort($arrayToSort, function($a, $b) use ($meta) {
return strcmp($a[$meta], $b[$meta]);
});
}
}
Call it:
Util::sortArrayByName($array, "array_property_name");
You can use usort like this
If you want to sort by number:
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
Or Abc char:
function cmp($a, $b)
{
return strcmp($a["fruit"], $b["fruit"]);
}
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");
See more: https://www.php.net/manual/en/function.usort.php
$array[0] = array('key_a' => 'z', 'key_b' => 'c');
$array[1] = array('key_a' => 'x', 'key_b' => 'b');
$array[2] = array('key_a' => 'y', 'key_b' => 'a');
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
usort($array, build_sorter('key_b'));
reference answer of Demodave to eating multi key
function array_sort_by(array $arr, $keys){
if(!is_array($keys))
$keyList = explode(',', $keys);
$keyList = array_keys(array_flip($keyList)); // array_unique
$keyList = array_reverse($keyList);
$result = &$arr;
foreach ($keyList as $key) {
if(array_key_exists($key, $arr))
$result = usort($result, function($a, $b) use ($key) { return strcmp($a->{$key}, $b->{$key}); });
}
return $result;
}
use this....
$array_list = [
"Apple" => 2,
"Pear" => 1,
"Orange" => 5,
"Lemon" => 1,
"Strawberry" => 2,
"Banana" => 3
];
function cmp($a, $b) {
return $b - $a;
}
$ao = new ArrayObject($object);
$ao->uasort('cmp');
print_r(json_encode($ao));
Bye!!!!
For my part, here is how I proceeded to sort an array of objects by object fields:
Code: (Demo) -- sorts by last_name ASC, then first_name ASC
<?php
$array = array(
(object)array(
'first_name' => 'Léa',
'last_name' => 'Weber',
),
(object)array(
'first_name' => 'Alexandre',
'last_name' => 'Dupont',
),
(object)array(
'first_name' => 'Léa',
'last_name' => 'Zotal',
),
(object)array(
'first_name' => 'Jérémie',
'last_name' => 'Hoffmann',
)
);
usort($array, function($a, $b) {
return [$a->last_name, $a->first_name]
<=>
[$b->last_name, $b->first_name];
});
var_export($array);
Outpout:
array (
0 =>
(object) array(
'first_name' => 'Alexandre',
'last_name' => 'Dupont',
),
1 =>
(object) array(
'first_name' => 'Jérémie',
'last_name' => 'Hoffmann',
),
2 =>
(object) array(
'first_name' => 'Léa',
'last_name' => 'Weber',
),
3 =>
(object) array(
'first_name' => 'Léa',
'last_name' => 'Zotal',
),
)
Arrow syntax with PHP7.4 and higher.
Makes sorting by multiple columns SUPER easy with the spaceship operator (<=>) aka the "Combined Comparison Operator" or "Three-way Comparison Operator".
Resource: https://wiki.php.net/rfc/combined-comparison-operator
https://stackoverflow.com/a/54647220/18090932

Categories