sort objects, some should always be first - php

I have a collection of country objects that look like this:
class country {
public $uid;
public $name;
}
Now I should sort them. One country with id == 999 should always be first in the collection, the rest should be sorted by name. So, I thought usort should actually do the trick, but the sorting is not correct. I tried this:
function mySortCallback($a, $b) {
if($a->uid == 999 || $b->uid == 999) return 1;
return strcmp($a->name, $b->name);
}
usort($myCollection, 'mySortCallback');

strcmp:
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.0 if they are equal.
Try this:
private function mySortCallback($a, $b) {
if ($a->uid == 999)
return -1;
elseif ($b->uid == 999)
return 1;
else
return strcmp($a->name, $b->name);
}

The line
if($a->uid == 999 || $b->uid == 999) return 1;
can't be correct in my opinion.
Try to exchange it to check whether actually just one of the two has uid==999 and return the value according to which of them has value 999.

Related

Can I use usort to sort primary by 1 value and secondary by second value?

So this function works great for sorting array or object by 1 value
function sort( $a, $b )
{
if( $a['1'] == $b['1'] ){ return 0 ; }
return ($a['1'] < $b['1']) ? -1 : 1;
}
usort($myArray,'sort');
But I would like it to sort by value 1 primary and by value 2 second, if this is possible with this function, or is it only doable with a custom function?
Sure, it's possible. To add an second sorting expression, you should extend the equal case $a['1'] == $b['1'].
You should also notice, you don't have to return exactly -1 or 1 - you could use any negative/positive number. So your sorter could be written as the following
usort(function($a, $b) {
if($a['1'] == $b['1']) {
return $a['2'] - $b['2'];
}
return $a['1'] - $b['1'];
}, 'sort');
I used this function. It is probably not the most efficient one, but for now it works.
function comp($a, $b) {
if ($a['1'] == $b['1']) {
return ($a['2'] < $b['2']) ? -1 : 1;
}
return strcmp($a['1'], $b['1']);
}
usort($arr, 'comp');

Dealing with equal numbers in usort PHP

I am trying to get PHP to compare two values and sort them using usort(). I have this function, which works, however this function stops running if $a == $b,
Having this function stop running prevents further functions in the PHP file to run.
<?php
function cmp($a, $b) {
if ($a[4] == $b[4]) {
return 0;
}
return ($a[4] < $b[4]) ? -1 : 1;
}
usort($participants_times,"cmp");
?>
When there are two values that are equal, I don't mind which one is in front of the other. I have tried setting return -1, return 0, and return 1 when $a == $b but they didnt work for me.
Any help is appreciated :)
You should replace ternary operator with nested if-else statements. In your condition, it returns 1 for both > and == comparison.
if ($a[4] < $b[4])
return -1;
else if ($a[4] > $b[4])
return 1;
else
return 0;
because you don't care the equal case, just ignore it
function cmp($a, $b) {
return ($a[4] < $b[4]) ? -1 : 1;
}
usort($participants_times,"cmp");
So the answers provided are very likely correct to my question - however, in my case, the issue was related to the form of my function()'s where I had a function inside a function and a second iteration of the initial function failed.
Moving my cmp() function outside of the function that calls it solved my issue.

Help with user defined sort function

function sort_searches($a, $b)
{
return
(
(isset($b['Class_ID']) && !isset($a['Class_ID']))
||
($b['Results'] && !$a['Results'])
||
(is_array($a['Results']) && !$a['Results'] && !is_array($b['Results']))
);
}
I'm using this function in usort(). The intended effect is that a list of searches will be sorted first by whether they have Class_IDs, and then second by results (with a non-empty array of results > results === false > results === empty array(). So a sorted set of searches would look like:
Class_ID with results
Class_ID with results === false
Class_ID with results === array()
No Class_ID with results
No Class_ID with results === false
No Class_ID with results === array()
Currently the functions sorts on results completely fine, but not on whether a search has a Class_ID.
usort($searches, 'sort_searches')
From the PHP docs:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Your function is not returning an integer.
To spell it out, let's say we wanted to write a sorting function for numbers (totally unnecessary, but for the exercise):
function sort_nums($a, $b)
{
if ($a < $b) return -1; // $a is less than $b
if ($a > $b) return 1; // $a is greater than $b
return 0; // $a is equal to $b
}

Help reverse my array sort comparison

I have my code working perfectly how I want it to, but the problem is my code is sorting by highest value to lowest. Can you help me reverse it so that when I print out the first 10 it is actually the "10 newest" (meaning the lowest duration)?
Thanks so much
function compareStreamDurations($a, $b)
{
if ($a["duration"] == $b["duration"])
{
return 0;
}
return ($a["duration"] > $b["duration"]) ? -1 : 1;
}
usort($onlineStreams, 'compareStreamDurations');
for ( $i=0; $i<10; $i++ )
{
echo '<p>', $onlineStreams[$i]["duration"] ,'</p>';
}
The solutions posted below (reversing the sign) are NOT working. I'm doing a print_r of $onlineStreams before and after the usort function call and they are both the same.
Just change your greater than to a less than:
return ($a["duration"] < $b["duration"]) ? -1 : 1;
Try reversing the greater than sign and making it a less than, like this:
function compareStreamDurations($a, $b)
{
if ($a["duration"] == $b["duration"])
{
return 0;
}
return ($a["duration"] < $b["duration"]) ? -1 : 1;
}

strcmp equivelant for integers (intcmp) in PHP

So we got this function in PHP
strcmp(string $1,string $2) // returns -1,0, or 1;
We Do not however, have an intcmp(); So i created one:
function intcmp($a,$b) {
if((int)$a == (int)$b)return 0;
if((int)$a > (int)$b)return 1;
if((int)$a < (int)$b)return -1;
}
This just feels dirty. What do you all think?
this is part of a class to sort Javascripts by an ordering value passed in.
class JS
{
// array('order'=>0,'path'=>'/js/somefile.js','attr'=>array());
public $javascripts = array();
...
public function __toString()
{
uasort($this->javascripts,array($this,'sortScripts'));
return $this->render();
}
private function sortScripts($a,$b)
{
if((int)$a['order'] == (int)$b['order']) return 0;
if((int)$a['order'] > (int)$b['order']) return 1;
if((int)$a['order'] < (int)$b['order']) return -1;
}
....
}
Sort your data with:
function sortScripts($a, $b)
{
return $a['order'] - $b['order'];
}
Use $b-$a if you want the reversed order.
If the numbers in question exceed PHP's integer range, return ($a < $b) ? -1 : (($a > $b) ? 1 : 0) is more robust.
Purely as some additional information, there has been an accepted RFC for this (https://wiki.php.net/rfc/combined-comparison-operator).
So, the comparison function would be along the lines of ...
<?php
$data = [...];
usort($data, function($left, $right){ return $left <=> $right; });
?>
A few really nice feature here is that the comparison is done in exactly the same way as all other comparisons. So type juggling will happen as expected.
As yet, there is no magic __forCompare() like method to allow an object to expose a comparison value. The current proposal (a different RFC) is to have each object be injected into every other object during the comparison so that it does the comparison - something which just seems odd to me - potential opportunity for recursion and stack overflow ... ! I would have thought either injecting the type of object for comparison (allowing an object the ability to represent appropriate values depending upon the type of comparison) or a blind request for a value that the object can serve up for comparison, would have been a safer solution.
Not yet integrated into PHP-NG (PHP 7 at the moment), but hopefully will be soon.
why reinventing the wheel?
http://php.net/manual/en/function.strnatcmp.php
echo strnatcmp(1, 2) . PHP_EOL; // -1
echo strnatcmp(10, 2) . PHP_EOL; // 1
echo strnatcmp(10.5, 2) . PHP_EOL; // 1 - work with float numbers
echo strnatcmp(1, -2) . PHP_EOL; // 1 - work with negative numbers
Test it here:
https://3v4l.org/pSANR
You could use
function intcmp($a,$b)
{
return ($a-$b) ? ($a-$b)/abs($a-$b) : 0;
}
Although I don't see the point in using this function at all
Does it have to be +1 and -1? If not, just return (int) $a - (int) $b. I don't like the divide that someone else recommended, and there's no need to check for all three cases. If it's not greater and not equal, it must be less than.
return (int) $a > (int) $b ? 1 : (int) $a == (int) $b ? 0 : -1;
At a glance, yes it feels dirty. Except there must be a good reason you wrote that instead of just using the actual ==, >, and < operators. What was the motivation for creating this function?
If it were me, I'd probably just do something like:
$x = $a==$b ? 0 : ($a>$b ? 1 : ($a<$b ? -1 : null));
I realize this is just as ugly, and the : null; - not sure if PHP requires it or if I could have just done :; but I don't like it and that code should never execute anyway... I think I'd be a lot less confused about this if I knew the original requirements!
For strings
usort($points, function ($a, $b) use ($orderFlag, $key1, $key2) {
return strcmp($a[$key1][$key2], $b[$key1][$key2]) * $orderFlag;
});
orderFlag => 1 (ascending): -1 (descending)
For numbers
usort($points, function ($a, $b) use ($orderFlag, $key1, $key2) {
return ($a[$key1][$key2] - $b[$key1][$key2]) * $orderFlag;
});
orderFlag => 1 (ascending): -1 (descending)

Categories