Sort customfield as numeric

Hi friends,

We have a customfield.{SortOrder} where users input numbers {1,2,3, 3.1, 3.3, 5,11, etc..}.

When i try to do OrderBy in the Result.html, they are sorted as string, causing 11 to be before 2.

I’ve tried using $OrderBy = ‘CAST(CustomField.{SortOrder} AS SIGNED)’; but its not sorting properly.

5 would come before 1 :melting_face:

Any idea?

Thanks!

Since RT 5.0 (I think) there is a support for sorting numerical customfields. But it’s not yet an easy configuration, you have to override RT::CustomField to mark the named CustomField as Numeric by returning true to the IsNumeric method: RT::CustomField - RT 6.0.2 Documentation - Best Practical

1 Like

To help, you need to create a local/lib/RT/CustomField_Local.pm with content like this (for a CF named “MyCustomField)”:

package RT::CustomField;

use strict;
no warnings qw(redefine);

sub IsNumeric {
    my $self = shift;
    return 1 if ( $self->Name eq 'MyCustomField' );
    return 0;
}

1;

make sure you restart your web service after.

5.0.6: RT 5.0.6 Release Notes - RT: Request Tracker - Best Practical

* Internal support to search/sort cf values numerically
* Support to calculate numeric custom fields in search charts

LIfe Saver! It works now! thanks!

1 Like

This is super useful! One extra thing to note - since RT 5.0.6 there’s built-in support for sorting custom fields numerically (so it won’t treat them like strings in search results) — but you do need to tell RT which CFs are numeric by overriding IsNumeric in a RT::CustomField_Local.pm and returning true for your field’s name. After that a restart should make the numeric sort work as expected.