Example #1
0
 /**
  * It should sort and preserve order for unchanging comparisons.
  */
 public function testSort()
 {
     $array = [['col1' => 20, 'col2' => 20], ['col1' => 20, 'col2' => 10], ['col1' => 10, 'col2' => 50], ['col1' => 10, 'col2' => 10], ['col1' => 10, 'col2' => 20]];
     $expected = [['col1' => 10, 'col2' => 50], ['col1' => 10, 'col2' => 10], ['col1' => 10, 'col2' => 20], ['col1' => 20, 'col2' => 20], ['col1' => 20, 'col2' => 10]];
     Sort::mergesort($array, function ($row1, $row2) {
         return strcmp($row1['col1'], $row2['col1']);
     });
     $this->assertEquals($expected, $array);
 }
Example #2
0
 /**
  * Process the sorting, also break sorting.
  *
  * @param array $table
  * @param Config $config
  *
  * @return array
  */
 private function processSort(array $table, Config $config)
 {
     if ($config['sort']) {
         $cols = array_reverse($config['sort']);
         foreach ($cols as $colName => $direction) {
             Sort::mergeSort($table, function ($elementA, $elementB) use($colName, $direction) {
                 if ($elementA[$colName] == $elementB[$colName]) {
                     return 0;
                 }
                 if ($direction === 'asc') {
                     return $elementA[$colName] < $elementB[$colName] ? -1 : 1;
                 }
                 return $elementA[$colName] > $elementB[$colName] ? -1 : 1;
             });
         }
     }
     if ($config['break']) {
         foreach ($config['break'] as $colName) {
             Sort::mergeSort($table, function ($elementA, $elementB) use($colName) {
                 if ($elementA[$colName] == $elementB[$colName]) {
                     return 0;
                 }
                 return $elementA[$colName] < $elementB[$colName] ? -1 : 1;
             });
         }
     }
     return $table;
 }