PHPackages                             tomkirsch/sorter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tomkirsch/sorter

ActiveProject[Utility &amp; Helpers](/categories/utility)

tomkirsch/sorter
================

Sorter Library for CI4

v1.3.2(2y ago)031MITPHPPHP ^7.3||^8.0

Since Feb 19Pushed 2y ago1 watchersCompare

[ Source](https://github.com/tomkirsch/ci4-sorter)[ Packagist](https://packagist.org/packages/tomkirsch/sorter)[ RSS](/packages/tomkirsch-sorter/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (1)Versions (20)Used By (0)

ci4-sorter
==========

[](#ci4-sorter)

Installation
------------

[](#installation)

Add service in `App\Config\Services`

```
	public static function sorter(array $tables=[], ?string $url=NULL, $getShared=TRUE){
		$sorter = $getShared ? static::getSharedInstance('sorter', $tables, $url) : new \Tomkirsch\Sorter\Sorter($tables, $url);
		return $sorter;
	}

```

Usage
-----

[](#usage)

In controller, you can access it with the service. You can pass an associative array to setup if you'd like.

```
	$sorter = service('Sorter');
	// or pass a configuration to save a step:
	$sorter = service('Sorter', [
		'foo'=>'foo_id asc',
		'bar'=>'bar_id asc',
	]);

```

To add tables, use the addTable() method:

```
	// add your tables. The "table name" is only used in GET parameters, so it doesn't need to match your database
	$sorter->addTable('foo', 'foo_id', 'asc');
	// you can pass the default sort using a space too:
	$sorter->addTable('bar', 'bar_id asc');

```

Just all getSort($tableName) to get the current sort field and direction. If none are passed, it'll use the default you specified above.

```
	// this quick version uses the first table
	$list = model('MyModel')->orderBy($sorter->getSort())->findAll();

	// if you have more than one table, you'll want to pass the name
	$list = model('MyModel')->orderBy($sorter->getSort('foo'))->findAll();

```

In your view, use QuickTable to configure columns, templates, and output the thead and tbody:

```
