Help with sorting examine query by multiple proper...
# help-with-umbraco
a
I have setup a property named "DisplayDate" of type DatePicker which I wish to be used as a sorting field in a query I am running in my frontend. This property is non-mandatory. So basically I am trying to sort posts descending first by the DisplayDate value, and then secondly by the core .CreateDate value . The purpose of the DIsplayDate property is just having a way to manually position nodes in a given position in listview / the query I am attempting to run. My current code seems that only sorty by "CreateDate", as no nodes which I know have a newer "DisplayDate" value are not being sorted first. Here is the snippet I am currently using: https://replit.com/@sjobidoo/QueryTest#MyController.cs Any advice pointing me in the right direction will be highly appriciated ❤️
j
Hmm so if you had one with a DisplayDate a year ago it would be before one without it but create date for today? 🤔 I think what I would do to get around this is to create a new examine field where you store the date, then in the event where you populate the field you check if the DisplayDate value is populated, then add that - otherwise add the createdate. Then you just have 1 field to sort on, where you know they will have a valid date as they always fallback to createdate
a
That's a clever suggestion. Seems simpler to work with . I'll just have to find a way to populate older nodes then, but that should be doable. Thanks for your input 🙂
j
If you add a transformingindexvalues event and then rebuild from the back office it will populate it for all nodes
d
Jemayn answered it, but I just want to add that, should you really need to sort on multiple properties, Examine has an orderby method that takes an array of fields to order by, so you can provide all fields in order of priority. Should you need to order by descending and order by ascending at the same time, the best way I found to do this, is to index the descending fields in reverse, that is: index
maxValue - actualValue
. Then you can sort the reverse field in ascending order and you end up sorting in descending order... if that makes sense
29 Views