Skip to content

Commit

Permalink
Fixing a sorting bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Moss committed Jan 28, 2014
1 parent 27fd21a commit c1f8b8d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/JamesMoss/Flywheel/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,21 @@ protected function sort(array $array, array $args)
$i = 0;
$cmp = 0;
while ($cmp == 0 && $i < $c) {
$valueA = $a->{$args[$i][0]};
$valueB = $b->{$args[$i][0]};
$keyName = $args[$i][0];
if($keyName == 'id') {
$valueA = $a->getId();
$valueB = $b->getId();
} else {
$valueA = isset($a->{$keyName}) ? $a->{$keyName} : null;
$valueB = isset($b->{$keyName}) ? $b->{$keyName} : null;
}

if (is_string($valueA)) {
$cmp = strcmp($valueA, $valueB);
} elseif (is_bool($valueA)) {
$cmp = $valueA - $valueB;
} else {
$cmp = ($valueA == $valueB) ? 0 : ($valueA > $valueB) ? -1 : 1;
$cmp = ($valueA == $valueB) ? 0 : (($valueA > $valueB) ? -1 : 1);
}

if ($args[$i][1] === SORT_DESC) {
Expand Down

0 comments on commit c1f8b8d

Please sign in to comment.