Sort by date as a numeric value

If I try to sort by date in the items/browse page, my results are sorted as strings and not as numeric values.

For example, if I sort alphabetically the three following dates in an ascendant order I would get :


Is there a way to sort it as a numeric value?


There’s no built-in way to do this: all values are stored as strings and sorted with MySQL’s regular indexing/sorting, which does just regular lexicographic sorting.

The usual workaround is to zero-pad dates so that the sorting works as expected. You could ltrim the zeroes back off in your theme or in a plugin’s element display filter if desired. If you went the plugin route you could also automate the zero-padding with an element save filter.

It’s possible to do this in other ways, like have MySQL cast the values to numbers and then sort, but this takes a big toll on sorting performance as indexing can’t be used. The better solution would be to have a separate column or table for the sort value as a number, with that indexed directly, and do the sort there. Either of these would need to be a plugin because they’d need SQL that Omeka’s core doesn’t do, and they’d need to hook into save events to keep the number copy of the data up to date.