---
title: Sorting
---
# Sorting
## Sorting in the View
You can add a form to capture sorting and filtering options together.
```erb
```
## Sorting in the Controller
To specify a default search sort field + order in the controller `index`:
```ruby
@search = Post.ransack(params[:q])
@search.sorts = 'name asc' if @search.sorts.empty?
@posts = @search.result.paginate(page: params[:page], per_page: 20)
```
Multiple sorts can be set by:
```ruby
@search = Post.ransack(params[:q])
@search.sorts = ['name asc', 'created_at desc'] if @search.sorts.empty?
@posts = @search.result.paginate(page: params[:page], per_page: 20)
```