From c1f8b8d8c221a3a31196888cd27d941284384bcc Mon Sep 17 00:00:00 2001 From: James Moss Date: Tue, 28 Jan 2014 10:19:52 +0000 Subject: [PATCH] Fixing a sorting bug. --- src/JamesMoss/Flywheel/Query.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/JamesMoss/Flywheel/Query.php b/src/JamesMoss/Flywheel/Query.php index 8e5481b..abf00df 100644 --- a/src/JamesMoss/Flywheel/Query.php +++ b/src/JamesMoss/Flywheel/Query.php @@ -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) {