Sort on DateIndex

To be able to sort via an index, this index must be very much "field index" like. It would, e.g., not work with a KeywordIndex or a TextIndex (because, an object may have more than a single value and therefore, may have more positions in the order). A DateIndex is quite like a FieldIndex (but not derived from it); it might be possible to use it in a sort (try it out).

Should it not work, you can use the same approach as for title/sortable_title, i.e. define a second index (this time a FieldIndex) alongside your DateIntex and use it for sorting.

Technical note: in order to be able to use an index for sorting, the index must provide the interface Products.PluginIndexes.interfaces.ISortIndex. This suggests the following alternative to the above index pair approach: use monkey patching to enhance the DateIndex by this interface. A DateIndex is sufficiently near to a FieldIndex, that it can use its implementation for this interface.

1 Like