Natural sorting of generated fields #18084
-
|
We are using generated fields to order entries by related categories like this: {myCategory.orderBy('level DESC').one().lft}When querying entries and sorting by the generated field: craft.entries.orderBy('customOrderBy ASC, title ASC')and outputting the
As noted in the Craft CMS docs (which even use a similar example), generated fields are stored and queried as text. That’s why the sorting is technically correct but not what we expect. We want a natural numeric sort.
Edit: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
You can do this using MySQL’s .orderBy('CAST([[customOrderBy]] AS UNSIGNED) ASC, title ASC')
That’s a known issue (#17938). I’m hoping to have it fixed soon. |
Beta Was this translation helpful? Give feedback.
You can do this using MySQL’s
CASTfunction:.orderBy('CAST([[customOrderBy]] AS UNSIGNED) ASC, title ASC')That’s a known issue (#17938). I’m hoping to have it fixed soon.