Sorting results by field value
A new View lists its entries by entry ID, from the oldest to the newest. You set a different order on the Filter & Sort tab of the View’s Settings box. In a Table or DataTables View, visitors can also click a column heading to sort the entries.
Set the order of a View #
- Edit the View.
- In the Settings box below the layout, click the Filter & Sort tab. If the box is missing, see The Settings box is hidden.
- Choose a field under Sort by field.
- Choose Ascending, Descending, or Random under Sort direction.
- To order entries that share the same value, choose a Sort by secondary field and its Secondary sort direction. A directory sorted by Last can use First as its secondary field, so that “Becker, Anna” comes before “Becker, Tom”. These two settings are hidden while Sort direction is Random.
- Click Update.

To list the newest entries first, choose Descending under Sort direction. Sort by field can stay on Default (Entry ID), or you can choose Date Created.
A View embedded with the [gravityview] shortcode can use its own order with the sort_field and sort_direction attributes, as Using the [gravityview] shortcode explains. In a DataTables View, sort_field works only with a field that is one of the table’s columns, or the field set under Sort by field.
How each kind of field sorts #
- Text fields, such as Single Line Text, Drop Down, and Email, sort alphabetically by the value stored in the entry. A Drop Down field sorts by the choice saved in each entry, whatever order the choices have in the form.
- Number and Total fields sort as numbers.
- Date fields sort from the oldest date to the newest when the direction is Ascending, and so do Date Created and Date Updated.
- Name fields are listed by their parts in Sort by field, such as First and Last.
- Fields inside a Repeater field are listed in Sort by field too. Each entry takes its place from the value in its first row, and an entry with no rows sorts like an entry with no value.
Entries with no value come first when the direction is Ascending.
Checkbox fields
Sort by field lists each choice of a Checkbox field on its own, by its label, such as Pottery. Sorting by a choice groups the entries that checked it: they come last with Ascending and first with Descending.
You can also sort a View by the whole Checkbox field. GravityView then joins the stored values of each entry’s checked choices, in the order the choices have in the form, and sorts the entries alphabetically by the result.
With the choices Pottery, Painting, and Woodwork, an entry with only Painting comes before an entry with only Pottery. An entry with Pottery and Painting comes right after the entries with only Pottery.
When Show Values is turned on for the field, each choice has a value of its own, and the order follows the values, whatever the labels say. In a DataTables View, this holds in Ajax (Server-side) mode.
To set the order yourself, give the choices values that sort the way you want, such as 1, 2, and 3 for Beginner, Intermediate, and Advanced. Entries already saved keep the values they had, as Changing or deleting choices in a form that already has entries explains.

To set this order, see Sort a View by a whole Checkbox field.
Product fields
A Single Product, Hidden Product, or Calculation field is listed by its parts in Sort by field: Name, Price, and Quantity. Price is stored as text with its currency symbol, such as $108.00, so it sorts alphabetically and $108.00 comes before $12.00.
To sort by price as a number, sort by the whole product field instead. A table sorted by a product column, or a [gravityview] shortcode with that field’s ID in sort_field, orders the entries by price and then by quantity. Prices and numbers sort alphabetically has more on numbers stored as text.
Let visitors sort a table #
In a Table View, turn on Enable sorting by column at the top of the Filter & Sort tab. Then each column that shows a form field has a sort link in its header, except List fields and Repeater fields, including each field inside a Repeater. Columns that show something other than a saved value have no sort link, such as Custom Content, Link to Single Entry, Number Sequence, and Edit Entry.
The first click on a column sorts it in ascending order, the second in descending order, and the third goes back to the View’s own order. When the View already sorts by that column in ascending order, the first click goes straight to descending order. Enabling the table column sorting feature shows the arrows on the page.
Visitors can sort a DataTables View by clicking a column heading, with no setting to turn on. How a DataTables View sorts its entries explains how its columns sort.
Sort a View by a whole Checkbox field #
Visitors sort by the whole field when they click the heading of a Checkbox column, in a Table View with Enable sorting by column on and in a DataTables View. In the [gravityview] shortcode, put the field’s ID in sort_field, such as sort_field="3", to sort the embedded View by the whole field. In a DataTables View, this works when the Checkbox field is one of the table’s columns.
To make the whole field the View’s own order, add it to the Sort by field and Sort by secondary field lists with this code. It adds every Checkbox field of the View’s form to both lists:
/**
* Adds each Checkbox field as a whole to the Sort by field and Sort by secondary field lists.
*
* @param array $fields The fields the sort lists offer, by field ID.
* @param int|array $form_id The form ID, or the form itself.
*
* @return array The fields, with every Checkbox field of the form added.
*/
add_filter( 'gravityview/common/sortable_fields', function ( $fields, $form_id ) {
$form = is_array( $form_id ) ? $form_id : GFAPI::get_form( $form_id );
if ( empty( $form['fields'] ) ) {
return $fields;
}
foreach ( $form['fields'] as $field ) {
if ( 'checkbox' === $field->type && ! isset( $fields[ (string) $field->id ] ) ) {
$fields[ (string) $field->id ] = [ 'label' => $field->label, 'type' => 'checkbox', 'parent' => null ];
}
}
return $fields;
}, 10, 2 );Note: Adding custom PHP code snippets to your website shows where to add this code.
With the code in place:
- Edit the View and click the Filter & Sort tab in the Settings box.
- Under Sort by field or Sort by secondary field, choose the Checkbox field. It’s listed by its label, after its choices.
- Choose its direction under Sort direction or Secondary sort direction, and click Update.

Keep the code on your site. Without it, the list shows Default (Entry ID) for that setting, and the next click on Update saves that order. The gravityview/common/sortable_fields page on gravitykit.dev lists the arguments of the filter the code uses.
Troubleshooting #
Random sorting doesn’t change the order
Random sorting uses the MySQL ORDER BY RAND() function, and some hosts turn it off. Random sorting not working on WP Engine shows how to turn it back on there. On another host, ask their support whether ORDER BY RAND() is allowed.
Dates sort in the wrong order
A date typed into a text field, such as Single Line Text, is stored as text and sorts alphabetically. Use a Date field for dates you want to sort by. For other orders that look wrong, see View sorting isn’t working and Forcing numbers to sort properly.