|
Posted by Ewoud Dronkert on 11/26/05 18:41
Ewoud Dronkert wrote:
> And, is there only one index value for each personid??
One salary figure, I mean. It do seems likely to have only one salary per
person, also the sorting wouldn't make sense otherwise.
A far better data structure would probably be, in pseudo code:
$person[] = array('id' => (int), 'name' => (string), 'salary' => (float));
Then the solution would be:
function cmp($a, $b) {
return $a['salary'] - $b['salary'];
}
Or a little safer:
function cmp($a, $b) {
$e = 0.01;
$t = $a['salary'] - $b['salary'];
if ( $t >= $e ) return 1;
if ( $t <= -$e ) return -1;
return 0;
}
Followed by:
usort($person, 'cmp');
foreach ( $person as $p )
printf('%3d %-20s %8.2f', $p['id'], $p['name'], $p['salary']);
--
E. Dronkert
Navigation:
[Reply to this message]
|