I am using this usort function to sort the array by value:
function cmp($a, $b)
{
if ($a["value"] == $b["value"])
{ return 0; }
return ($a["value"] < $b["value"]) ? -1 : 1;
}
Now, each object also has an "active" field as well, that is: $a["active"]. I would like to have the sorted array to have first the active ones (1) ordered by value, then, when all of them are sorted, the inactive (0) ones. I tried using another usort after this but it doesn't work nicely, it somewhat scrambles them.
Thanks in advance.
function cmp($a,$b){
if ($a["active"]==$b["active"]){
if($a["value"]==$b["value"])
return 0;
return $a["value"]>$b["value"])?1:-1;
}
return $a["active"]>$b["active"]?1:-1;
}
Untested - but I think it'll work. A good resource for this sort of stuff (but not this particular example, unfortunately for you): http://www.the-art-of-web.com/php/sortarray/
Simply add the active stuff as another comparison in your function:
if ($a['active'] > $b['active'])
return 1;
} else if ($a['value'] == $b['value']) {
return 0;
} else {
return ($a['value'] < $b['value']) ? -1 : 1;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.