Sorting | laravel-query-builder-go | Spatie       

 SPATIE  

laravel-query-builder-go
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-query-builder-go](https://spatie.be/docs/laravel-query-builder-go/v1)  Features  Sorting

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-query-builder-go/v1) 

  Sorting    
- [ Requirements ](https://spatie.be/docs/laravel-query-builder-go/v1/requirements)
- [ Introduction ](https://spatie.be/docs/laravel-query-builder-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-query-builder-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-query-builder-go/v1/questions-issues)
- [ Testing ](https://spatie.be/docs/laravel-query-builder-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-query-builder-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-query-builder-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-query-builder-go/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-query-builder-go/v1/about-us)

Features
--------

- [ Filtering ](https://spatie.be/docs/laravel-query-builder-go/v1/features/filtering)
- [ Sorting ](https://spatie.be/docs/laravel-query-builder-go/v1/features/sorting)
- [ Including relationships ](https://spatie.be/docs/laravel-query-builder-go/v1/features/including-relationships)
- [ Selecting fields ](https://spatie.be/docs/laravel-query-builder-go/v1/features/selecting-fields)

Advanced usage
--------------

- [ Extending query builder ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/extending-query-builder)
- [ Pagination ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/pagination)
- [ Multi value delimiter ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/multi-value-delimiter)
- [ Front-end implementation ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/front-end-implementation)
- [ Differences from PHP ](https://spatie.be/docs/laravel-query-builder-go/v1/advanced-usage/port-differences)

 Sorting
=======

###  On this page 

1. [ Basic usage ](#content-basic-usage)
2. [ Disallowed sorts ](#content-disallowed-sorts)
3. [ Custom sorts ](#content-custom-sorts)
4. [ Using an alias for sorting ](#content-using-an-alias-for-sorting)

Basic usage
-----------------------------------------------------------------------------------------

The `sort` query parameter determines which property to sort by. Allowed sorts must be explicitly specified with `AllowedSorts`.

```
// GET /users?sort=name,-score
users, err := querybuilder.For(model.Query[User](), input).
    AllowedSorts(querybuilder.FieldSort[User]("name"), querybuilder.FieldSort[User]("score")).
    Get(r)
```

Sorts are ascending by default. A `-` prefix requests descending order. Multiple sorts apply in request order. Existing ordering on the subject remains before request ordering, matching PHP's append behavior. Start with an unordered subject when clients should choose the primary sort.

For a default sort, call `DefaultSorts(querybuilder.FieldSort[User]("-score"))`. Defaults apply only when the request contains no sorts. A default does not expose its name to request input. Register it separately with `AllowedSorts` when clients should also be able to request it.

Disallowed sorts
--------------------------------------------------------------------------------------------------------

Unknown sort names return a `*QueryError` with `Kind == "sorts"`. Set `IgnoreUnknownSorts` in the config to ignore unknown names. Unknown input never becomes a database identifier. An ignored unknown request sort still suppresses defaults, as it is an explicit request.

Custom sorts
--------------------------------------------------------------------------------------------

Use a function or method value for a custom sort:

```
length := querybuilder.CustomSort("name-length",
    func(q *model.Builder[User], descending bool) (*model.Builder[User], error) {
        if descending {
            return q.OrderByRaw("LENGTH(name) DESC"), nil
        }
        return q.OrderByRaw("LENGTH(name) ASC"), nil
    },
)
```

Only the allowlisted public name comes from the request. Raw SQL expressions must be application-owned. Errors from custom sorts propagate unchanged. A leading minus on `CustomSort` sets its direction when used as a default. An explicit request chooses its own direction.

Using an alias for sorting
--------------------------------------------------------------------------------------------------------------------------------------

```
querybuilder.FieldSort[User]("public_name", "name")
```

Clients request `sort=public_name` or `sort=-public_name`. The internal column is never inferred from client input.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
