Jump to content

Help with uSort?


matstars

Recommended Posts

I have an array, $employee, that outputs as such

[1] => Array

(

[name] => Michael Smith

[age] => 34

[experience] => 4.3

)

 

[2] => Array

(

[name] => Brandon Singh

[age] => 28

[experience] => 7.4

)

 

[3]

(

[name] => Robert Sant

[age] => 40

[experience] => 17.3

)

 

I am trying to sort by Experience ascending and descending - I tried the following, but it doesn't work, can anyone help?

 

function columnSort($unsorted, $column) {

$sorted = $unsorted;

for ($i=0; $i < sizeof($sorted)-1; $i++) {

for ($j=0; $j<sizeof($sorted)-1-$i; $j++)

if ($sorted[$j][$column] > $sorted[$j+1][$column]) {

$tmp = $sorted[$j];

$sorted[$j] = $sorted[$j+1];

$sorted[$j+1] = $tmp;

}

}

return $sorted;

}

 

function columnSortReverse($unsorted, $column) {

$sorted = $unsorted;

for ($i=0; $i < sizeof($sorted)-1; $i++) {

for ($j=0; $j<sizeof($sorted)-1-$i; $j++)

if ($sorted[$j][$column] < $sorted[$j+1][$column]) {

$tmp = $sorted[$j];

$sorted[$j] = $sorted[$j+1];

$sorted[$j+1] = $tmp;

}

}

return $sorted;

}

 

$employee["name"]="Michael Smith";

$employee["age"]=34;

$employee["experience"]=4.3;

 

$employee=array(

array(Name=>"Michael Smith", Age=>34, Exp=>4.3),

array(Name=>"Brandon Singh", Age=>28, Exp=>7.4),

array(Name=>"Robert Sant", Age=>40, Exp=>17.3)

);

 

echo var_dump($employee);

echo "<BR>";

print_r(columnSort($employee, 'Experience'));

echo "<BR><BR>";

print_r(columnSortReverse($employee, 'Experience'));

echo "<BR><BR>";

 

 

 

Thanks,

 

Mat

 

Link to comment
https://forums.phpfreaks.com/topic/137026-help-with-usort/
Share on other sites

Actually using usort():

<?php

$employee=array(
array(	'Name'	=>"Michael Smith",
		'Age'	=> 34,
		'Exp'	=>4.3
	 ),
array(	'Name'	=> "Brandon Singh",
		'Age'	=> 28,
		'Exp'	=> 7.4
	 ),
array(	'Name'	=> "Robert Sant",
		'Age'	=> 40,
		'Exp'	=> 17.3
	 )
);

function ExpAsc($v1,$v2) {
    if ($v1['Exp'] == $v2['Exp']) {
        return 0;
    }
    return ($v1['Exp'] < $v2['Exp']) ? -1 : 1;
}

function ExpDesc($v1,$v2) {
    if ($v1['Exp'] == $v2['Exp']) {
        return 0;
    }
    return ($v1['Exp'] < $v2['Exp']) ? 1 : -1;
}

echo "<pre>";
echo var_dump($employee);
echo "</pre><hr /><pre>";
usort($employee, 'ExpAsc');
print_r($employee);
echo "</pre><hr /><pre>";
usort($employee, 'ExpDesc');
print_r($employee);
echo "</pre>";

?>

Link to comment
https://forums.phpfreaks.com/topic/137026-help-with-usort/#findComment-715657
Share on other sites

Is there any way I can specify what I want to sort in the function itself?  Or would i have to create a function for Experience, Age and Name to sort them?

Not within usort itself, although you can create a set of ExpAsc(), AgeAsc(), NameAsc(), ExpDesc(),... callback functions; and then set the appropriate callback name as a variable within your code:

function employeeSort($employeeArray,$column,$ascDesc) {
   $callback = $column.$ascDesc;
   usort($employeeArray, $callback);
   return $employeeArray;
}

$employee = employeeSort($employee,'Age','Desc');
[code]


The alternative is to look at the array_multisort() function

Link to comment
https://forums.phpfreaks.com/topic/137026-help-with-usort/#findComment-715698
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.