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

Method sortRows

lib/Service/FlexibleStorageService.php:971–999  ·  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

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

Callers 1

queryMethod · 0.95

Calls 1

decimalCompareMethod · 0.95

Tested by

no test coverage detected