From fd719b692003dcfd92f9982299d5d65a0fa0689e Mon Sep 17 00:00:00 2001 From: Timothy Marois Date: Mon, 11 Dec 2017 09:18:40 -0500 Subject: [PATCH] Fixed ASC sort order (previous patch only fixed DESC). This now allows Natural Sort Order of query orderBy(). --- CHANGELOG.md | 3 +++ src/Database.php | 2 +- src/QueryLogic.php | 11 ++++------- tests/QueryTest.php | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a3a6c2..6312227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Change Log ========== +### 12/11/2017 - 1.0.11 +* Fixed query `sort` which allows for "natural order", issues before would assume "1" and "10" are equal in value, but this has been resolved with this update. Uses php `strnatcasecmp()`, This was fixed for `DESC` order in the previous update. This patch fixes the `ASC` sort order. + ### 12/10/2017 - 1.0.10 * Fixed query `sort` which allows for "natural order", issues before would assume "1" and "10" are equal in value, but this has been resolved with this update. Uses php `strnatcasecmp()` * Added argument `results( false )` - `false` on `results()` method that allows it to return the full document object or (by default = `true`) only the document data. diff --git a/src/Database.php b/src/Database.php index 26b2589..6dc9eea 100644 --- a/src/Database.php +++ b/src/Database.php @@ -10,7 +10,7 @@ class Database * Stores the version of Filebase * use $db->getVersion() */ - const VERSION = '1.0.10'; + const VERSION = '1.0.11'; //-------------------------------------------------------------------- diff --git a/src/QueryLogic.php b/src/QueryLogic.php index 84a5a6b..9243544 100644 --- a/src/QueryLogic.php +++ b/src/QueryLogic.php @@ -188,18 +188,15 @@ protected function sort() usort($this->documents, function($a, $b) use ($orderBy, $sortBy) { + $propA = $a->field($orderBy); + $propB = $b->field($orderBy); + if ($sortBy == 'DESC') { - $propA = $a->field($orderBy); - $propB = $b->field($orderBy); - - // strnatcasecmp allows us to test in "natural" order return strnatcasecmp($propB, $propA) <=> strnatcasecmp($propA, $propB); - - // return $b->field($orderBy) <=> $a->field($orderBy); } - return $a->field($orderBy) <=> $b->field($orderBy); + return strnatcasecmp($propA, $propB) <=> strnatcasecmp($propB, $propA); }); diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 9c000d9..1155692 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -399,6 +399,26 @@ public function testSorting() $this->assertEquals(['Amex','Hooli','Microsoft'], [$test5[0]['name'],$test5[1]['name'],$test5[2]['name']]); $db->flush(true); + + $companies = ['Google 9', 'Google 3', 'Google 10', 'Google 1', 'Google 2', 'Google 7']; + + foreach($companies as $company) + { + $user = $db->get(uniqid()); + $user->name = $company; + $user->save(); + } + + // order the results ASC (but inject numbers into strings) + $test6 = $db->query()->limit(3)->orderBy('name', 'ASC')->results(); + $this->assertEquals(['Google 1','Google 2','Google 3'], [$test6[0]['name'],$test6[1]['name'],$test6[2]['name']]); + + // order the results DESC (but inject numbers into strings) + $test6 = $db->query()->limit(3)->orderBy('name', 'DESC')->results(); + $this->assertEquals(['Google 10','Google 9','Google 7'], [$test6[0]['name'],$test6[1]['name'],$test6[2]['name']]); + + $db->flush(true); + }