Jump to content
Larry Ullman's Book Forums

Having Trouble Understaning The Sort Functions


Recommended Posts

I'm having trouble understanding how the array is being sorted.

Specifically; each echo statement is the same so how does it know which function to echo?

Thanks

 




$students = array (
256 => array ('name' => 'Jon', 'grade' => 98.5),
2 => array ('name' => 'Vance', 'grade' => 85.1),
9 => array ('name' => 'Stephen', 'grade' => 94.0),
364 => array ('name' => 'Steve', 'grade' => 85.1),
68 => array ('name' => 'Rob', 'grade' => 74.6)
);


// Name sorting function:
function name_sort ($x, $y) {
return strcasecmp($x['name'], $y['name']);
}

// Grade sorting function:
// Sort in DESCENDING order!
function grade_sort ($x, $y) {
return ($x['grade'] < $y['grade']);
}

// Print the array as is:
echo '<h3>Array As Is</h3><pre>' . print_r($students, 1) . '</pre>';

// Sort by name:
uasort ($students, 'name_sort');

// Print the array now:
echo '<h3>Array Sorted By Name</h3><pre>' . print_r($students, 1) . '</pre>';

// Sort by grade:
uasort ($students, 'grade_sort');
*/
// Print the array now:
echo '<h3>Array Sorted By Grade</h3><pre>' . print_r($students, 1) . '</pre>';

?>
</body>
</html>

Link to comment
Share on other sites

It's because of the sorting before the echo statements. :)

 

 

// NORMAL SORTING

// Print the array as is:

echo '<h3>Array As Is</h3><pre>' . print_r($students, 1) . '</pre>';

 

// Sort by name:

uasort ($students, 'name_sort');

 

// Print the array now:

echo '<h3>Array Sorted By Name</h3><pre>' . print_r($students, 1) . '</pre>';

// Sort by grade:

uasort ($students, 'grade_sort');

 

// Print the array now:

echo '<h3>Array Sorted By Grade</h3><pre>' . print_r($students, 1) . '</pre>';

  • Upvote 1
Link to comment
Share on other sites

Thanks for your reply.

 

Do you mean that if I change the order (put the echo before the function) it won't work?

 

I've always had trouble understanding the syntax of functions.

 

It's because of the sorting before the echo statements. :)

 

 

// NORMAL SORTING

// Print the array as is:

echo '<h3>Array As Is</h3><pre>' . print_r($students, 1) . '</pre>';

 

// Sort by name:

uasort ($students, 'name_sort');

 

// Print the array now:

echo '<h3>Array Sorted By Name</h3><pre>' . print_r($students, 1) . '</pre>';

// Sort by grade:

uasort ($students, 'grade_sort');

 

// Print the array now:

echo '<h3>Array Sorted By Grade</h3><pre>' . print_r($students, 1) . '</pre>';

Link to comment
Share on other sites

I had that too. It's actually very simple.

 

// A normal variable containing a number
$a-number = 10;

// This is 10 because it's before we use the function
echo $a-number;

// Now we use our function (see bottom) on this variable
add_ten($a-number);

// This is now 20 because of the function below
echo $a-number;

// This is a function that adds ten to the value
function add_ten($function-variable) {
  return $function-variable + 10;
}

 

It does not matter what variable you put into the "use" part of the function. It can be named anything. That is why it's great. You could have a variable called "$another-number" and add ten to that as well. :)

  • Upvote 1
Link to comment
Share on other sites

Think of it as meaning "one element from the $students array that we shall call $x (or anything else)" and "another element from the $students array that we shall call $y (or anything else)". So the function will take for instance $students[9][name] (value: "Stephen") and compare it with $students[2][name] (value: "Vance"); and then go on comparing pairs of elements from the array to decide which must come first.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...