5

The client has a large number of ‘Product Offer’ components which they need fetched so I wrote a wrapper to the CD API to allow the fetching of these offers by metadata, more specifically by metadata fields and values passed in as a HashMap. They requested that we sort the returned Component Presentations based on a custom metadata field called ‘Priority’, which of course is no problem. We could simply call addSorting on the query object passing in a new sortParameter and sort them by their literal String values such as below.

CustomMetaKeyColumn customMetaKeyPriority = new CustomMetaKeyColumn(metadataField, MetadataType.STRING);
query.addSorting(new SortParameter(customMetaKeyPriority, SortParameter.ASCENDING));

However, when there is a conflict (for instance we are returned the top 5 CPs with highest priority) we want the Component Presentations sorted by their last modification date so we get the most recently updated. Is there any way that I can apply multiple sort parameters so that when we run into priority conflicts, we can resolve these conflicts using another sort parameter?

Note that I found this answer ‘https://stackoverflow.com/questions/11228580/applying-more-than-one-sort-on-a-tridion-broker-query’, but it did definitively answer my question.

6

You can in fact apply more than one sortParameter to resolve conflicts. The initial sorting that is applied to the query will put the Component Presentations in order based on the sorting parameter. When the next SortParameter is applied, I found that the priorities were not completely shuffled out of order (resorted) but the subsets of CPs (grouped on priority) were then sorted based on the second sorting parameter. The following should serve as an example of resolving conflicts.

CustomMetaKeyColumn customMetaKeyPriority = new CustomMetaKeyColumn(metadataField, MetadataType.STRING);
query.addSorting(new SortParameter(customMetaKeyPriority, SortParameter.ASCENDING));
ItemModificationColumn customMetaKeyDate = new ItemModificationColumn();
query.addSorting(new SortParameter(customMetaKeyDate, SortParameter.DESCENDING));
itemURIs = query.executeQuery();

To prove my point, I have include two snippets of debugging output. The first are some metadata fields printed out from the sorted Component Presentations. Note that the conflicts are not sorted by modification date but are in fact sorted by priority (ascending).

ID: 6218    Modified: 2013-09-23 16:35:34.0     Priority: 002
ID: 6215    Modified: 2013-09-16 14:02:09.0     Priority: 003
ID: 6232    Modified: 2013-09-16 15:40:08.0     Priority: 003
ID: 6234    Modified: 2013-09-24 14:12:41.0     Priority: 003
ID: 6235    Modified: 2013-09-16 15:55:48.0     Priority: 003
ID: 6217    Modified: 2013-09-23 18:29:56.0     Priority: 006

The second excerpt has had both sorting parameters applied to the query, with the initial ascending priority sort being applied first following with conflicts successfully being sorted by last modification date (descending).

ID: 6218    Modified: 2013-09-23 16:35:34.0     Priority: 002
ID: 6234    Modified: 2013-09-24 14:12:41.0     Priority: 003
ID: 6235    Modified: 2013-09-16 15:55:48.0     Priority: 003
ID: 6232    Modified: 2013-09-16 15:40:08.0     Priority: 003
ID: 6215    Modified: 2013-09-16 14:02:09.0     Priority: 003
ID: 6217    Modified: 2013-09-23 18:29:56.0     Priority: 006
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.