-1

I want to sort on field banner_id (int). But when running I get a strange result.

The result of this usort() is following:

101 - 204- 34 - 45 - 69 - etc.

I must get:

34 - 45 -69 - 101 - 204

function usort_reorder($a,$b){
    $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'banner_id'; 
    $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; 
    $result = strcmp($a[$orderby], $b[$orderby]); 
    return ($order==='asc') ? $result : -$result; 
}

usort($data, 'usort_reorder');

What to do to get this sort correct running. Means not only sorting the first digit but the whole digit. Now it only takes the first one.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Hermants
  • 231
  • 1
  • 3
  • 9

1 Answers1

0
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.

You can check this link for more understanding strcmp equivelant for integers (intcmp) in PHP