The following usort function does not always give the right result since it will only "push" up or down one position relative to the compared item. Thus when performing the sort multiple times the result Yes No Yes Nocan occur.
The function successfully sort field b.
How can I solve this?
array
[0] => array("a"=>"Yes","b"=>"apple"...);
[1] => array("a"=>"Yes","b"=>"banana"...);
[2] => array("a"=>"No","b"=>"lemon"...);
[3] => array("a"=>"No","b"=>"grape"...);
...
current function
function sortAnserDesc($x, $y){
if ( $x['a'] == $y['a'] )
return 0;
else if ( $x['a'] < $y['a'] )
return 1;
else
return -1;
}
I found a very good function on http://www.php.net/manual/en/function.usort.php#103722
And I think you have a general problem.
I try to display the callback function for usort here.
function usortByArrayKey($key, $asc=SORT_ASC) {
$sort_flags = array(SORT_ASC, SORT_DESC);
if (!in_array($asc, $sort_flags))
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
return function(array $a, array $b) use ($key, $asc, $sort_flags) {
if (!is_array($key)) { //just one key and sort direction
if (!isset($a[$key]) || !isset($b[$key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$key] == $b[$key])
return 0;
return ($asc == SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
} else { //using multiple keys for sort and sub-sort
foreach ($key as $sub_key => $sub_asc) {
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if (!in_array($sub_asc, $sort_flags)) {
$sub_key = $sub_asc;
$sub_asc = $asc;
}
//just like above, except 'continue' in place of return 0
if (!isset($a[$sub_key]) || !isset($b[$sub_key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$sub_key] == $b[$sub_key])
continue;
return ($sub_asc == SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
};
}
And to integrate with your code, you might have something like:
Sorting a value only by DESCENDING.
usort($YOUR_ARRAY, usortByArrayKey('a', SORT_DESC));
Sorting a and b.
usort($YOUR_ARRAY, usortByArrayKey(array('a', 'b')));
More on sorting a and b by DESCENDING
usort($YOUR_ARRAY, usortByArrayKey(array('a', 'b')), SORT_DESC);
Hope this help!!
Why don't you directly use strcmp function?
function sortAnserDesc($x, $y){
return strcmp($x['a'],$y['a']);
}
Related
I am trying to sort an array based on a specific key but it's not working. The array is below when in JSON format. I want to sort it in ascending order by id_question.
This is what I have done so far:
public function compare($ar1, $ar2){
if ($ar1['id_question']<$ar2['id_question']) {
return 1;
}else {
return -1;
}
}
Call the sort function:
uasort($related, Array ($this, 'compare'));
This is what it returns:
As you can see, it doesn't apply the sort.
It's done
here is solution
usort($related, function($a, $b){
if ($a['id_question'] < $b['id_question']) {
return -1;
}else {
return 1;
}
});
I hope this helps -
$listItem = collect($related)->sortBy('id_question')->toArray();
Please try:
$related = collect($related)->sortBy('id_question')->all();
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
I've got an array as such:
$currentTerm = array(
'name' => $term->name,
'weight' => $term->weight
);
I'd like to sort the entire array by the weight key. I can't figure out how to accomplish this. Any thoughts or should I write a custom function for this?
P.S Some weights can be the same; so storing them as a key isn't an option. And it's multidimensional with a children key which is filled recursively the same way as above.
You can create a custom sorting method usort().
It would work similarly to this:
usort($array, function($a,$b) {
if($a->weight === $b->weight) return 0;
return ($a->weight < $b->weight) ? -1 : 1;
});
If people from the PHP7 future come here, they can just use this (spaceships!):
usort($array, function($a,$b) {
return $a->weight <=> $b->weight;
});
A full (and cumbersome) example for pre-PHP7 would be:
<?php
class Obj {
public $name;
public $weight;
function __construct($name,$weight) {
$this->name = $name;
$this->weight = $weight;
}
}
$array = array(
new Obj("A",3),
new Obj("B",10),
new Obj("C",8),
);
usort($array, function($a,$b) {
if($a->weight == $b->weight) return 0;
return ($a->weight < $b->weight) ? -1 : 1;
});
print_r($array);
?>
Take a look at array_multisort()
$weight = array();
foreach ($currentTerm as $key => $row)
{
$weight[$key] = $row['weight'];
}
then
array_multisort($weight, SORT_ASC, $currentTerm);
or
array_multisort($weight, SORT_DESC, $currentTerm);
You can try this code:
//define a comparison function
function cmp($a, $b) {
if ($a['status'] == $b['status']) {
return 0;
}
return ($a['status'] < $b['status']) ? -1 : 1;
}
usort($array, "cmp");
function sort_multi_array($array, $key)
{
if (is_null($array)) return 0;
$keys = array();
for ($i=1;$i<func_num_args();$i++) {
$keys[$i-1] = func_get_arg($i);
}
// create a custom search function to pass to usort
$func = function ($a, $b) use ($keys) {
for ($i=0;$i<count($keys);$i++) {
if ($a[$keys[$i]] != $b[$keys[$i]]) {
return ($a[$keys[$i]] > $b[$keys[$i]]) ? -1 : 1;
}
}
return 0;
};
usort($array, $func);
return $array;
}
I'm building a simple search query however when it reaches the end i.e. no more entries in Warning: usort() expects parameter 1 to be array, null given in
How can I test to see if the array is empty and simply return a null result before it reaches the usort line?
thank you!
Check before using usort if the $array is null or not.
if ($array !== NULL) {
usort($array, $func);
}
Add a check for empty data -
if (!empty($array)) {
// process data
}
I wanted to sort php array based on CRITICAL , WARNING ,INFO sub string and then CRITICAL , WARNING ,INFO sub array should be sorted again with the time stamp value contains in each line of string in acsending order.
Basically at the end I need array to be sorted with CRITICAL 1st with time stamp sorted then WARNING and then INFO so on..
First, define a function that turns the urgency of a line into a number.
function urgency($line)
{
if (strpos($line, 'INFO') !== false) {
return 1;
} elseif (strpos($line, 'WARNING') !== false) {
return 2;
} elseif (strpos($line, 'CRITICAL') !== false) {
return 3;
}
return 0;
}
Then, assuming each element of your array contains a line of the file, you need to apply a decorator to keep the sort stable; see also my earlier answer on the subject:
array_walk($array, function(&$element, $index) {
$element = array($element, $index); // decorate
});
After applying the decorator, you sort the array; I'm using a stable comparison helper:
function stablecmp($fn)
{
return function($a, $b) use ($fn) {
if (($tmp = call_user_func($fn, $a[0], $b[0])) != 0) {
return $tmp;
} else {
return $a[1] - $b[1];
}
};
}
usort($array, stablecmp(function($a, $b) {
return urgency($b) - urgency($a);
}));
Finally, undecorate the array to produce the end result:
array_walk($array, function(&$element) {
$element = $element[0];
});
Getting CRITICAL on the sorted order
function my_cmp($a, $b){
$pieces_a = explode("CRITICAL", $a);
$pieces_b = explode("CRITICAL", $b);
if(!isset($pieces_a[1]) && isset($pieces_b[1])) {
return 1;
}
elseif(!isset($pieces_b[1]) && isset($pieces_a[1])) {
return -1;
}
elseif(!isset($pieces_a[1]) && !isset($pieces_b[1])) {
return 0;
}
return strcasecmp($pieces_a[1], $pieces_b[1]);
}
usort($arr, "my_cmp");
But this can only sort if the each line has non spaces I mean single word,.
Any other solution curious to know?
I tried to search and found this:
Sort an array by a child array's value in PHP
But the function does not work in my case:
$sorted = array();
foreach($players as $player)
{
$p = Model::factory('user');
$p->load($player['id']);
$sorted[] = array('id' => $player['id'], 'username' => $p->get_username());
}
How can i sort the array alphabetic after the username?
The function,
function cmp($a, $b) {
if ($a['username'] == $b['username']) {
return 0;
}
return ($a['username'] < $b['username']) ? -1 : 1;
}
and then calling usort($sorted,"cmp"); will not work out for me (getting error undefined index[2])..
And is there any way to choose whether it should be sorting descending or ascending?
The 'cmp' function will be:
// $param - the parameter by which you want to search
function cmp(&$a, &$b, $param) {
switch( $param ) {
case 'id':
if ( $a['id'] == $b['id'] ) {
return 0;
}
return ( $a['id'] < $b['id'] ) ? -1 : 1;
break;
case 'username':
// string comparison
return strcmp($a['username'], $b['username']);
break;
}
}
// this is the sorting function by using an anonymous function
// it is needed to pass the sorting criterion (sort by id / username )
usort( $sorted, function( $a,$b ) {
return cmp( $a, $b, 'username');
});
Because the index 2 doesn't exist in your array. You should use $a['username'] or $a['id'], but i guess you want to sort on username so then you would use $a['username'].