Commit 0f05f0fc by Scott

Optimize qa_sort_by

Avoid extra comparison function.
parent 4f8dc92b
...@@ -46,16 +46,27 @@ function qa_sort_by_fn($a, $b) ...@@ -46,16 +46,27 @@ function qa_sort_by_fn($a, $b)
{ {
global $qa_sort_by_1, $qa_sort_by_2; global $qa_sort_by_1, $qa_sort_by_2;
$compare = qa_sort_cmp($a[$qa_sort_by_1], $b[$qa_sort_by_1]); // if the first keys are equal we can sort by the second keys only
$sortkey = $qa_sort_by_1;
if ($a[$sortkey] == $b[$sortkey]) {
if (!isset($qa_sort_by_2))
return 0;
if ($compare == 0 && $qa_sort_by_2) $sortkey = $qa_sort_by_2;
$compare = qa_sort_cmp($a[$qa_sort_by_2], $b[$qa_sort_by_2]); }
$av = $a[$sortkey];
$bv = $b[$sortkey];
return $compare; if (is_numeric($av) && is_numeric($bv)) // straight subtraction won't work for floating bits
return $av == $bv ? 0 : ($av < $bv ? -1 : 1);
else
return strcasecmp($av, $bv); // doesn't do UTF-8 right but it will do for now
} }
/** /**
* General comparison function for two values, textual or numeric * General comparison function for two values, textual or numeric
* @deprecated
*/ */
function qa_sort_cmp($a, $b) function qa_sort_cmp($a, $b)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment