Ticket #3080 (new enhancement)
Opened 6 years ago
Last modified 14 months ago
Custom field sorts only as text
| Reported by: | aroach@… | Owned by: | jonas |
|---|---|---|---|
| Priority: | normal | Milestone: | next-major-0.1X |
| Component: | general | Version: | 0.9.5 |
| Severity: | normal | Keywords: | tracobject fieldrefactoring |
| Cc: | jhn@… | ||
| Release Notes: | |||
| API Changes: | |||
Description
I've created a custom field to enable me to manually order tickets. I've used integers as the value, but it seems to be sorting as text.
For instance, the order shows up like 1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9
Would like to be able to sort numerical values through custom fields.
Attachments
Change History
comment:1 Changed 5 years ago by sid
comment:2 Changed 5 years ago by cboos
- Keywords tracobject added
- Milestone set to 1.0
More custom field types are needed.
comment:3 Changed 3 years ago by richard.dunlap@…
In particular, types for userID (resulting in a combo box populated by Trac user ids) and datetime (entry TBD, but displayed as an offset from current datetime a la the creation and last update datetimes) would be very useful.
comment:4 Changed 2 years ago by bobbysmith007@…
I will second the need for numeric custom fields.
If they were added and a reasonable patch for the query module were submitted, would this be likely to be accepted?
comment:5 Changed 2 years ago by anonymous
+1 for this fix. The lack of sorting impacts the efficiency of http://trac-hacks.org/wiki/TimingAndEstimationPlugin
comment:6 Changed 2 years ago by jhn@…
- Cc jhn@… added
i'd like to see this too. i've read through FieldRefactoring and GenericTrac, and while they sound interesting, they are long-term. one of the best things about trac is its extensibility and customizability, and this enhancement could have so many different applications.
in our installation, we would use this for ranking of tickets. we use priority but it is not granular enough. our client ranks their top 20 tickets and we work our way down the list. we used the 00, 01, 02 workaround suggested by sid but we would prefer to have real numeric sorting.
this approach makes sense to me:
add a new type to TracTicketsCustomFields, "number" or "integer".
if a field is set to this type, just use a numeric sort on it with some simple error handling. the data could still be stored as a string in the backend so it seems that the changes would be limited.
comment:7 Changed 2 years ago by anonymous
recommend changing the summary from "Custom field sorts only as text" to "add numeric type to custom field types".
comment:8 Changed 23 months ago by CraigBarber@…
As a workaround, you can modify the ORDER BY clause in your reports with something similar to the following (for SQLite):
LEFT JOIN ticket_custom as some_custom_field ON some_custom_field.name='some_custom_field' ... ORDER BY CASE WHEN some_custom_field.value ISNULL THEN 99999999 ELSE CAST(some_custom_field.value AS INTEGER) END
The CASE/ISNULL/99999999 sorts NULLs after all other defined values. You could just use CAST(...) if you don't care about NULL order.
comment:9 Changed 22 months ago by cboos
- Milestone changed from 1.0 to unscheduled
Milestone 1.0 deleted
comment:10 Changed 14 months ago by achton@…
If you only have numeric custom fields, another (dangerous) workaround is to alter the column type of the 'value' column in ticket_custom to float/integer.
It may have adverse effects on the precision and visual output of the column content, but should allow proper numeric sorting. Use with care.
/achton
comment:11 Changed 14 months ago by cboos
- Keywords fieldrefactoring added
- Milestone changed from unscheduled to next-major-0.1X
#9929 proposes to introduce pluggable sort orders, for version and milestone fields. Something we could fit into the FieldRefactoring spec and generalize to any field.



What is the type you have defined for the field in trac.ini? Looking at TracTicketsCustomFields, there is no integer type for a custom field, so the sorting you are seeing is probably the expected behavior.
A hack to get around this is to pad the field with zeros. So 01, 02, 03, ..., 10, 11.