MCPcopy Create free account
hub / github.com/Rello/analytics / sortRows

Method sortRows

lib/Service/FlexibleStorageService.php:974–1002  ·  view source on GitHub ↗

* @param list > $data * @param list ,aggregation:string}> $projection * @param list ,direction:string}> $sort * @return list > */

(array $data, array $projection, array $sort)

Source from the content-addressed store, hash-verified

972 * @return list<list<mixed>>
973 */
974 private function sortRows(array $data, array $projection, array $sort): array {
975 if ($sort === []) {
976 return $data;
977 }
978 $indexes = array_flip(array_column(array_column($projection, 'column'), 'ref'));
979 $decorated = array_map(static fn (array $row, int $index): array => ['row' => $row, 'index' => $index], $data, array_keys($data));
980 usort($decorated, function (array $left, array $right) use ($sort, $indexes): int {
981 foreach ($sort as $item) {
982 $index = $indexes[$item['column']['ref']];
983 $a = $left['row'][$index] ?? null;
984 $b = $right['row'][$index] ?? null;
985 if ($a === $b) {
986 continue;
987 }
988 if ($a === null || $b === null) {
989 $comparison = $a === null ? -1 : 1;
990 } elseif ($item['column']['role'] === 'measure' || in_array($item['column']['type'], ['decimal', 'boolean'], true)) {
991 $comparison = $this->decimalCompare((string)$a, (string)$b);
992 } else {
993 $comparison = strcmp((string)$a, (string)$b);
994 }
995 if ($comparison !== 0) {
996 return $item['direction'] === 'DESC' ? -$comparison : $comparison;
997 }
998 }
999 return $left['index'] <=> $right['index'];
1000 });
1001 return array_column($decorated, 'row');
1002 }
1003
1004 private function decimalAdd(string $left, string $right): string {
1005 [$leftSign, $leftDigits, $leftScale] = $this->decimalParts($left);

Callers 1

queryMethod · 0.95

Calls 1

decimalCompareMethod · 0.95

Tested by

no test coverage detected