Repository: flow-php/flow (PHP)

Issue:
Sorting by column works in opposite way when external sort exceeds allowed memory
It looks that when whole dataset to sort is small enough to fit into allowed memory for external sort implementation then sorting works fine (sorry for overcomplicated example, I was testing multiple theories):
```php
// putenv('FLOW_EXTERNAL_SORT_MAX_MEMORY=10M');
$arr = [];
for ($j = 100; $j > 0; $j--) {
    for ($i = 200; $i > 0; $i--) {
        $arr[] = ['id' => str_pad((string) $j, 5, '0', STR_PAD_LEFT).'-'.str_pad((string) $i, 3, '0', STR_PAD_LEFT)];
    }
}

shuffle($arr);

data_frame()
    ->read(from_array($arr))
    ->sortBy(ref('id'))
    ->limit(10)
    ->write(to_output(truncate: true))
    ->run();
```
Output:
```
+-----------+
|        id |
+-----------+
| 00001-001 |
| 00001-002 |
| 00001-003 |
| 00001-004 |
| 00001-005 |
| 00001-006 |
| 00001-007 |
| 00001-008 |
| 00001-009 |
| 00001-010 |
+-----------+
```

But when we set memory limit to anything below amount required to run sorting (example above reaches ~60MB for my tests), by e.g. uncommenting `putenv('FLOW_EXTERNAL_SORT_MAX_MEMORY=10M');` the output for the same code as above is:
```
+-----------+
|        id |
+-----------+
| 00100-200 |
| 00100-199 |
| 00100-198 |
| 00100-197 |
| 00100-196 |
| 00100-195 |
| 00100-194 |
| 00100-193 |
| 00100-192 |
| 00100-191 |
+-----------+
```

I believe it may be a bug and should work the same, no matter if we use (filesystem) cache to support sorting or not, right? 🙂

Thanks in advance for taking a look into this!


Discussion: hey, from top of my head I can say that it's very possible that it might work differently since after reaching certain predicted memory consumption, different algorithm is being used which might cause discrapencies. 
I'm gonna take a look into this, thank you for the code samples and expected output, that will make it a lot easier for me to reproduce 💪 
hey, quick heads up, I didn't had yet time to look into this as I was occupied with another, a bit more critical [nasty bug](https://github.com/flow-php/flow/pull/1117) 



Fix (patch):
diff --git a/src/core/etl/src/Flow/ETL/ExternalSort/RowsMinHeap.php b/src/core/etl/src/Flow/ETL/ExternalSort/RowsMinHeap.php
index aa57b40ee..363077238 100644
--- a/src/core/etl/src/Flow/ETL/ExternalSort/RowsMinHeap.php
+++ b/src/core/etl/src/Flow/ETL/ExternalSort/RowsMinHeap.php
@@ -45,17 +45,22 @@ public function insert(mixed $value) : void
      */
     protected function compare($value1, $value2) : int
     {
-        $values1 = [];
-        $values2 = [];
+        $leftValues = [];
+        $rightValues = [];
 
         foreach ($this->ref as $entry) {
             $row1Value = $value1->row()->valueOf($entry->name());
             $row2Value = $value2->row()->valueOf($entry->name());
 
-            $values1[] = $entry->sort() === SortOrder::ASC ? $row1Value : -$row1Value;
-            $values2[] = $entry->sort() === SortOrder::ASC ? $row2Value : -$row2Value;
+            if ($entry->sort() === SortOrder::DESC) {
+                $leftValues[] = $row1Value;
+                $rightValues[] = $row2Value;
+            } else {
+                $leftValues[] = $row2Value;
+                $rightValues[] = $row1Value;
+            }
         }
 
-        return $values1 <=> $values2;
+        return $leftValues <=> $rightValues;
     }
 }