Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sort to the Dataset Array #5

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"ByJG\\AnyDataset\\Lists\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
Expand Down
46 changes: 46 additions & 0 deletions src/ArrayDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\AnyDataset\Core\IteratorFilter;
use ByJG\AnyDataset\Core\Row;

class ArrayDataset
{
Expand Down Expand Up @@ -64,4 +65,49 @@ public function getIterator(IteratorFilter $filter = null): GenericIterator
{
return new ArrayDatasetIterator($this->array, $filter, $this->propertyIndexName, $this->propertyKeyName);
}

/**
*
* @param string $field
* @return void
*/
public function sort(string $field): void
{
if (count($this->array) == 0) {
return;
}

$this->array = $this->quickSortExec($this->array, $field);
}

/**
* @param Row[] $seq
* @param string $field
* @return array
*/
protected function quickSortExec(array $seq, string $field): array
{
if (!count($seq)) {
return $seq;
}

$key = $seq[0];
$left = $right = array();

$cntSeq = count($seq);
for ($i = 1; $i < $cntSeq; $i ++) {
if (($seq[$i][$field] ?? null) <= ($key[$field] ?? null)) {
$left[] = $seq[$i];
} else {
$right[] = $seq[$i];
}
}

return array_merge(
$this->quickSortExec($left, $field),
[ $key ],
$this->quickSortExec($right, $field)
);
}

}
46 changes: 35 additions & 11 deletions tests/ArrayDatasetTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
<?php

namespace Tests\AnyDataset\Lists;
namespace Tests;

use ByJG\AnyDataset\Core\Enum\Relation;
use ByJG\AnyDataset\Core\IteratorFilter;
use ByJG\AnyDataset\Core\IteratorInterface;
use ByJG\AnyDataset\Core\Row;
use ByJG\AnyDataset\Lists\ArrayDataset;
use PHPUnit\Framework\TestCase;
use Tests\AnyDataset\Sample\ModelGetter;
use Tests\AnyDataset\Sample\ModelPublic;
use UnexpectedValueException;

require_once "Sample/ModelGetter.php";
require_once "Sample/ModelPublic.php";
use Tests\Sample\ModelGetter;
use Tests\Sample\ModelPublic;

class ArrayDatasetTest extends TestCase
{
Expand Down Expand Up @@ -189,7 +185,7 @@ public function testnavigateFromModel1()
$sr = $arrayIterator->moveNext();
$this->assertField($sr, "__id", 0);
$this->assertField($sr, "__key", 0);
$this->assertField($sr, "__class", "Tests\\AnyDataset\\Sample\\ModelPublic");
$this->assertField($sr, "__class", "Tests\\Sample\\ModelPublic");
$this->assertField($sr, "id", 1);
$this->assertField($sr, "name", 'ProdA');
$count++;
Expand All @@ -198,7 +194,7 @@ public function testnavigateFromModel1()
$sr = $arrayIterator->moveNext();
$this->assertField($sr, "__id", 1);
$this->assertField($sr, "__key", 1);
$this->assertField($sr, "__class", "Tests\\AnyDataset\\Sample\\ModelPublic");
$this->assertField($sr, "__class", "Tests\\Sample\\ModelPublic");
$this->assertField($sr, "id", 2);
$this->assertField($sr, "name", 'ProdB');
$count++;
Expand All @@ -207,7 +203,7 @@ public function testnavigateFromModel1()
$sr = $arrayIterator->moveNext();
$this->assertField($sr, "__id", 2);
$this->assertField($sr, "__key", 2);
$this->assertField($sr, "__class", "Tests\\AnyDataset\\Sample\\ModelPublic");
$this->assertField($sr, "__class", "Tests\\Sample\\ModelPublic");
$this->assertField($sr, "id", 3);
$this->assertField($sr, "name", 'ProdC');
$count++;
Expand Down Expand Up @@ -235,7 +231,7 @@ public function testnavigateFilterFromModel1()
$sr = $arrayIterator->moveNext();
$this->assertField($sr, "__id", 1);
$this->assertField($sr, "__key", 1);
$this->assertField($sr, "__class", "Tests\\AnyDataset\\Sample\\ModelPublic");
$this->assertField($sr, "__class", "Tests\\Sample\\ModelPublic");
$this->assertField($sr, "id", 2);
$this->assertField($sr, "name", 'ProdB');
$count++;
Expand Down Expand Up @@ -374,5 +370,33 @@ public function testPropertyKeyNameEmpty()
$row = $iterator->moveNext();
$this->assertEquals(["value" => 'ProdA'], $row->toArray());
}

public function testSort()
{
$array = [
['name' => 'joao', 'age' => 41],
['name' => 'fernanda', 'age' => 45],
['name' => 'jf', 'age' => 15],
['name' => 'jg jr', 'age' => 4]
];

$dataset = new ArrayDataset($array);

$this->assertEquals([
['__id' => 0, '__key' => 0, 'name' => 'joao', 'age' => 41],
['__id' => 1, '__key' => 1, 'name' => 'fernanda', 'age' => 45],
['__id' => 2, '__key' => 2, 'name' => 'jf', 'age' => 15],
['__id' => 3, '__key' => 3, 'name' => 'jg jr', 'age' => 4]
], $dataset->getIterator()->toArray());

$dataset->sort('age');

$this->assertEquals([
['__id' => 0, '__key' => 0, 'name' => 'jg jr', 'age' => 4],
['__id' => 1, '__key' => 1, 'name' => 'jf', 'age' => 15],
['__id' => 2, '__key' => 2, 'name' => 'joao', 'age' => 41],
['__id' => 3, '__key' => 3, 'name' => 'fernanda', 'age' => 45],
], $dataset->getIterator()->toArray());
}
}

3 changes: 2 additions & 1 deletion tests/Sample/ModelGetter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\AnyDataset\Sample;
namespace Tests\Sample;

use ByJG\Serializer\BaseModel;

Expand All @@ -23,6 +23,7 @@ public function __construct($Id, $Name)
{
$this->_Id = $Id;
$this->_Name = $Name;
parent::__construct();
}

public function getId()
Expand Down
3 changes: 2 additions & 1 deletion tests/Sample/ModelPublic.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\AnyDataset\Sample;
namespace Tests\Sample;

use ByJG\Serializer\BaseModel;

Expand All @@ -20,5 +20,6 @@ public function __construct($Id, $Name)
{
$this->Id = $Id;
$this->Name = $Name;
parent::__construct();
}
}
Loading