Repository: mathesar-foundation/mathesar

Issue:
Sort list of schemas
On the Database Page, the schemas appear in random order.

![image](https://user-images.githubusercontent.com/42411/199863005-2cc007c5-3224-489b-86de-80d924010833.png)

I think they should be listed alphabetically, perhaps with `public` first.

I'm not sure whether it would be better to handle this on the back end or the front end.

Any thoughts @mathemancer @rajatvijay?


Update:
* This issue is supposed to be handled on the backend.
* Part of this issue is solved in https://github.com/centerofci/mathesar/pull/2062
* Refer https://github.com/centerofci/mathesar/issues/1897#issuecomment-1470542703 for the current state of the issue.


Discussion:
Is this necessary for the live demo?
@kgodey 

> Is this necessary for the live demo?

I don't feel strongly about it. Feel free to re-prioritize
@seancolsen Can I tackle this issue!
Sure, go ahead @ayao451!
Given how easy is to create a schema from FE, I expect this list to grow in the future. It will be better if we handle it on the BE, I think the effort is going to be somewhat similar
> Given how easy is to create a schema from FE, I expect this list to grow in the future. It will be better if we handle it on the BE, I think the effort is going to be somewhat similar

Also, we're paginating (or can paginate) the results on the back end (if there are enough of them). This would require sorting them consistently in the back end.
I removed the frontend label from the issue.
Hi @kgodey! This is actually my first ever issue, and I was wondering about the specifics of the fix you wanted me to implement! Since this is a backend task, I wanted to sort the schemas in the actual database, so when we display it, it should automatically display sorted! Would this be the way you wanted me to tackle it!
Yes, we should sort the schemas in the backend. @mathemancer can point to the best place in the code to do this.
Thank you so much! I am excited to contribute!!
A couple of things:

- The correct place to start looking at ordering for the schemas is [the schema viewset](https://github.com/centerofci/mathesar/blob/69aab25c88e282bddb0e323b8b50f26ff6053f5a/mathesar/api/db/viewsets/schemas.py)
- Currently, they're ordered in descending order based on creation timestamp (newest first). It occurs to me that this (or most recently accessed) might be at least as valid as alphabetical.

At the risk of overly increasing the scope of this ticket, it seems like alphabetical should be one of a few options for ordering. (others being creation date, and recently accessed), and the default should actually be the current ordering (creation date descending).

@seancolsen What do you think? As a user has more and more schemas they're working with, I don't think they'll want to always have to go to the 4th page of schema listings to find their Movie collection schema.

Apologies for not thinking deeply enough about this in my earlier reply. I didn't realize we already had a pretty sensible (to me) default ordering.
I think alphabetical still makes the most sense here, it's easiest to scan and a user is probably not indexing on when they created a schema.

> As a user has more and more schemas they're working with, I don't think they'll want to always have to go to the 4th page of schema listings to find their Movie collection schema.

We're going to introduce some sort of bookmarking / favoriting in the future to solve this problem. 

> At the risk of overly increasing the scope of this ticket, it seems like alphabetical should be one of a few options for ordering. (others being creation date, and recently accessed), and the default should actually be the current ordering (creation date descending).

I do think offering more sorting options makes sense in the future (out of scope for this issue), but the default should still be alphabetical.

Edit: or actually the default should be determined through usability testing.
I can try to code up all four for user-testing purposes then! I am also a bit new to git, so should I create my own branch and maybe put alphabetical sorting there, as well as create separate branches for the other sorting methods? Once I finish the sorting, I can try to get a button on the front end that can toggle the sort, although as the database grows, it would take much longer! 
@ayao451 For this issue, just alphabetical sorting is fine, no need to create all four or make any front end changes. We're just discussing ideas for things we could do in the future.

Please do make your own branch, change the code to do alphabetical sorting, and then create a pull request. We'll review it once it's up.
Sounds good! Would the change just be one small thing then, Changing this to -name instead of created_at?
`def get_queryset(self):
        qs = Schema.objects.all().order_by('-created_at')
        return self.access_policy.scope_viewset_queryset(self.request, qs)`

You'd want to do `name`, not `-name`, since `-` changes it to descending order. When you're sorting by date, descending makes sense since you want newest first. When you're sorting by name, you want ascending.

Edit: see [Django docs for `order_by` for more details](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#order-by)
Hi @kgodey I made my commit to my branch! It is called ayaobranch, should I run git push?
@ayao451 Yes, push your changes and make a pull request as [described here](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork).

Resolving patch:
diff --git a/mathesar/api/db/viewsets/schemas.py b/mathesar/api/db/viewsets/schemas.py
--- a/mathesar/api/db/viewsets/schemas.py
+++ b/mathesar/api/db/viewsets/schemas.py
@@ -22,7 +22,7 @@ class SchemaViewSet(AccessViewSetMixin, viewsets.GenericViewSet, ListModelMixin,
     access_policy = SchemaAccessPolicy
 
     def get_queryset(self):
-        qs = Schema.objects.all().order_by('-created_at')
+        qs = Schema.objects.all().order_by('name')
         return self.access_policy.scope_viewset_queryset(self.request, qs)
 
     def create(self, request):


Test patch: