Repository: statamic-rad-pack/runway (php)

Issue:
Resource listing sort order is not applied correctly
### Description

Sort ordering in the admin panel isn't being applied unless an order has already been applied by the `runwayListing` scope.

In the method `\StatamicRadPack\Runway\Http\Controllers\CP\ResourceListingController::index(...)` the following check only successfully applies a user selected sort order _if_ an order has already been applied to the query.

```php
        $query->when($query->getQuery()->orders, function ($query) use ($request) {
            if ($request->input('sort')) {
                $query->reorder($request->input('sort'), $request->input('order'));
            }
        }, fn ($query) => $query->orderBy($resource->orderBy(), $resource->orderByDirection()));
```

One can see that `$query->getQuery()->orders` will be `null` unless `$query->orderBy(...)` has already been called.

### Steps to reproduce

1. Attempt to apply a non-default sort order in the admin panel for a model that doesn't call `$query->orderBy(...)` in it's `runwayListing` scope.

See https://github.com/statamic-rad-pack/runway/commit/c9ec2aec506a195782f18c4924f84ce990266ecb for an examlpe test that reproduces this issue.

### Environment

Tests have been executed outside of an application environment.

Pull request description:
[6.x] Bring back handling user selection when there is no other orderby
This [change](https://github.com/statamic-rad-pack/runway/pull/434/commits/0321d67eba5b65089926e1f52df56117aa49fa82) reverted some of the logic for handling user selected sorting.

This PR brings it back.

Closes #437 

Patch:
diff --git a/src/Http/Controllers/CP/ResourceListingController.php b/src/Http/Controllers/CP/ResourceListingController.php
index ab52ac4..b69bc39 100644
--- a/src/Http/Controllers/CP/ResourceListingController.php
+++ b/src/Http/Controllers/CP/ResourceListingController.php
@@ -30,7 +30,7 @@ class ResourceListingController extends CpController
             if ($request->input('sort')) {
                 $query->reorder($request->input('sort'), $request->input('order'));
             }
-        }, fn ($query) => $query->orderBy($resource->orderBy(), $resource->orderByDirection()));
+        }, fn ($query) => $query->orderBy($request->input('sort', $resource->orderBy()), $request->input('order', $resource->orderByDirection())));
 
         $activeFilterBadges = $this->queryFilters($query, $request->filters, [
             'resource' => $resource->handle(),


Test patch:
diff --git a/tests/Http/Controllers/CP/ResourceListingControllerTest.php b/tests/Http/Controllers/CP/ResourceListingControllerTest.php
index 8ea1ef3..dfd7aa4 100644
--- a/tests/Http/Controllers/CP/ResourceListingControllerTest.php
+++ b/tests/Http/Controllers/CP/ResourceListingControllerTest.php
@@ -138,6 +138,32 @@ class ResourceListingControllerTest extends TestCase
             ]);
     }
 
+    /** @test */
+    public function listing_rows_are_ordered_when_user_defines_an_order_and_no_runway_listing_scope_order_exists()
+    {
+        $user = User::make()->makeSuper()->save();
+        $posts = Post::factory()->count(2)->create();
+
+        $this
+            ->actingAs($user)
+            ->get(cp_route('runway.listing-api', ['resource' => 'post', 'sort' => 'id', 'order' => 'desc']))
+            ->assertOk()
+            ->assertJson([
+                'data' => [
+                    [
+                        'title' => $posts[1]->title,
+                        'edit_url' => "http://localhost/cp/runway/post/{$posts[1]->id}",
+                        'id' => $posts[1]->id,
+                    ],
+                    [
+                        'title' => $posts[0]->title,
+                        'edit_url' => "http://localhost/cp/runway/post/{$posts[0]->id}",
+                        'id' => $posts[0]->id,
+                    ],
+                ],
+            ]);
+    }
+
     /** @test */
     public function can_search()
     {