Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Fixed ASC sort order (previous patch only fixed DESC). This now allow…
Browse files Browse the repository at this point in the history
…s Natural Sort Order of query orderBy().
  • Loading branch information
timothymarois committed Dec 11, 2017
1 parent 19de7a7 commit fd719b6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Database
* Stores the version of Filebase
* use $db->getVersion()
*/
const VERSION = '1.0.10';
const VERSION = '1.0.11';


//--------------------------------------------------------------------
Expand Down
11 changes: 4 additions & 7 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

});

Expand Down
20 changes: 20 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}


Expand Down

0 comments on commit fd719b6

Please sign in to comment.