# Sort Process

Comparative function

nameincome
John \$50,000
Marry \$60,000
Peter \$100,000
Donald \$80,000
``````
->pipe(new Sort(array(
"income"=>function(\$a, \$b) {
if (\$a < \$b) return 1;
else if (\$a === \$b) return 0;
else if (\$a > \$b) return -1;
}
)))
``````
nameincome
Peter \$100,000
Donald \$80,000
Marry \$60,000
John \$50,000

The example demonstrates usage of `Sort` process. The sort process is used to sort data according to a comparative function.

Sort using comparative function

``````"income"=>function(\$a, \$b) {
if (\$a < \$b) return 1;
else if (\$a === \$b) return 0;
else if (\$a > \$b) return -1;
}
``````
``````<?php
require_once "MyReport.php";

\$report = new MyReport;
\$report->run()->render();
``````
``````<?php

use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Sort;

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
protected function settings()
{
return array(
"dataSources"=>array(
"data"=>array(
"class"=>'\koolreport\datasources\ArrayDataSource',
"dataFormat"=>"table",
"data"=>array(
array("name","income"),
array("John",50000),
array("Marry",60000),
array("Peter",100000),
array("Donald",80000),
)
)
)
);
}
protected function setup()
{
//Prepare data
\$this->src("data")
->pipe(new ColumnMeta(array(
"income"=>array(
"type"=>"number",
"prefix"=>"\$"
)
)))
->saveTo(\$source);

//Save orginal data
\$source->pipe(\$this->dataStore("origin"));

//Pipe through process to get result
\$source->pipe(new Sort(array(
"income"=>function(\$a, \$b) {
if (\$a < \$b) return 1;
else if (\$a === \$b) return 0;
else if (\$a > \$b) return -1;
}
)))
->pipe(\$this->dataStore("result"));
}
}``````
``````<?php
use \koolreport\widgets\koolphp\Table;
?>
<div class="report-content">
<div class="text-center">
<h1>Sort Process</h1>
</div>

<?php
Table::create(array(
"dataSource"=>\$this->dataStore("origin"),
"cssClass"=>array(
"table"=>"table-bordered table-striped table-hover"
)
));
?>

<i class="fa fa-arrow-down" style="font-size:24px;"></i>
<pre style="font-weight:bold"><code>
->pipe(new Sort(array(
"income"=>function(\$a, \$b) {
if (\$a < \$b) return 1;
else if (\$a === \$b) return 0;
else if (\$a > \$b) return -1;
}
)))
</code></pre>
<i class="fa fa-arrow-down" style="font-size:24px;"></i>

<div style="margin-top:20px;">
<?php
Table::create(array(
"dataSource"=>\$this->dataStore("result"),
"cssClass"=>array(
"table"=>"table-bordered table-striped table-hover"
)
));
?>
</div>

</div>``````

### What People Are Saying

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"
-- Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great and amazing."
-- Dr. Lew Choy Onn

"Fantastic framework for reporting!"
-- Greg Schneider