Sort data in multiple array
I have an array like this:
$B = array(
array("firstnam" =>"A", "lastname"=>"D", "grade" =开发者_运维知识库> "k1", "score" => 50),
array("firstnam" =>"B", "lastname"=>"C", "grade" => "k4", "score" => 20),
array("firstnam" =>"C", "lastname"=>"B", "grade" => "k3", "score" => 10),
array("firstnam" =>"D", "lastname"=>"A", "grade" => "k2", "score" => 80)
);
If you're trying to sort h1-3 by h4, try this:
array_multisort($h4, SORT_NUMERIC, SORT_ASC, $h3, SORT_NUMERIC, SORT_ASC, $h2, SORT_NUMERIC, SORT_ASC, $h1, SORT_NUMERIC, SORT_ASC);
If this isn't what you're looking for, try rephrasing your question.
public function sortByHeader($columns, $sortOrder=1/* or -1 for desc */) {
$this->sort_columns = $columns;
$this->sort_order = $sortOrder;
uasort($this->data, array($this, "cmp"));
}
public function cmp($a, $b) {
foreach ($this->sort_columns as $column) {
// It's also important to check datatypes if you have string and it in diff columns
if ($a[$column] > $b[$column]) {
return $this->sort_order;
}
if ($a[$column] < $b[$column]) {
return -$this->sort_order;
}
}
return 0;
}
uasort allows to use custom callback for sorting your data. See PHP man for reference
精彩评论