JReviews logo
Guides My site
← All guides Community guide

Sort listing values numerically

J Alejandro
Thread started by Jackson. Contributors Alejandro.
Thread started
Last updated

What you’ll achieve

Sort listings using the numeric values stored in custom fields.

My site

Platform
JReviews version

Sort listing values numerically by using a numeric custom field, and optionally round that field's value in the edit form. Rounding a text input does not make its ordering numeric.

Use a numeric field for numeric ordering

Store values that must sort numerically in a numeric custom field. A text field sorts as text, so descending text order can place 950 before 1500. Rounding the form value does not change the sorting behavior of a text field.

Use a listing field of type Integer for whole-number references or Decimal when fractions matter. For this price example, create a listing Decimal field labeled Price, with the exact name jr_price. Select a listing field group named Product details in the field editor. In your Product listing type's Field Groups settings, associate that same group. You can reuse a group already associated with Product.

Keep Price active, optional, and without dependencies. Give product editors write access and visitors read access. Editors enter a number such as 950, 1500, or 10.236, without currency signs or thousands separators. Do not create predefined options.

In that listing type's list-page settings, set Order by field to jr_price for ascending prices, or rjr_price for descending prices. Substitute your numeric field name. This sets the default order for that listing type.

For example, use jr_price for prices. Open the list page with order=jr_price in its query string for ascending order, or order=rjr_price for descending order. Add ? before the parameter when the URL has no query string, or & when it already has one. Replace an existing order parameter instead of adding a second one.

Create or choose listings storing 950 and 1500. Ascending order should show 950 first. Descending order should show 1500 first. Check the values saved in the numeric field before changing an existing field setup. A field label alone does not change how its stored data sorts.

JReviews 5 and v6 Legacy

Add the code to your overrides

Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.

WordPress. Use jreviews_overrides/filters/filter_functions.php.

Start a new file with <?php followed by defined('MVC_FRAMEWORK') or die;. Add the examples within that PHP file without repeating its opening tag or guard.

Round the price shown in the edit form

If a numeric field displays more decimal places than users need, add this field_form:jr_price filter to the override file above. It rounds the value supplied to the form input to two decimal places.

Clickfwd\Hook\Filter::add('field_form:jr_price', function($input, $field)
{
    if (!empty($input['search'])) {
        return $input;
    }
    if (isset($input['value'][0]) && is_numeric($input['value'][0])) {
        $input['value'][0] = round((float) $input['value'][0], 2);
    }

  return $input;
});

Replace jr_price in the hook name with your numeric field name. Change the 2 in round() to the required decimal precision.

The filter does not change the database column or force a fixed two-digit display such as 10.00. Test editing and saving the field before using it on existing listings.

JReviews 6 Early Access

Round the price shown in an edit form

The native numeric-sorting steps above work independently of Early Access. For this optional rounding hook, open Configuration > Early Access Settings and enable Listing Form. The hook changes the modern frontend form for an existing listing.

If you want that edit form to show an existing price rounded to two decimal places, filter the Price input's rendered value.

Joomla. Save the code in templates/jreviews_overrides/hooks.php.

WordPress. Save the code in jreviews_overrides/hooks.php.

Start a new file with <?php. Add the example below without repeating the opening tag if the file already exists. Keep any existing hooks.

fwd_add_filter('jreviews:view_data:forms_components_text_input', function (array $data): array {
    $input = $data['component'] ?? null;
    $listing = fwd_request()->attributes->get('jreviews:listing');
    if (!fwd_request()->inFrontend()
        || !$listing instanceof \JReviews\App\Models\Listing
        || !$listing->exists
        || !$input instanceof \JReviews\App\View\Components\Forms\TextInput
        || $input->getStatePath() !== 'fields.jr_price') {
        return $data;
    }
    $value = $input->getState();
    if ($value !== null && $value !== '' && is_numeric($value)) {
        $data['getState'] = fn () => round((float) $value, 2);
    }
    return $data;
}, 20, 1);

Replace jr_price in fields.jr_price with the name of your numeric field. The example rounds only that input on an existing listing's frontend form. It leaves new listings, search inputs, control-panel forms, empty values, nonnumeric values, and other fields unchanged.

This changes the value displayed in the input. It does not change the database column or force trailing zeros such as 10.00. Saving a displayed rounded value can update the stored value, so decide whether that is appropriate for your field.

Test an existing price such as 10.236. Its edit input should show 10.24. Check a blank price, another field, and a new listing to confirm that they retain their original values.

Learn more