Here’s a quick way to implement custom sort functionality in a Content Query web part. Since the out-of-the-box options only offer ascending or descending alphanumeric sorting, you can use a Calculated Column to provide the necessary logic.
For example, say you have a list called ‘Defects’ that has the following two Choice fields: ‘Impact’ and ‘Status’.
Impact = {“Red, “Yellow”, “Green”}
Status = {“Open”, “Closed”}
The requirement is for the Impact field to be sorted in the following order: “Red”, “Yellow”, “Green”. All “Open” items should appear before the “Closed” items.
So what to do…?
We can use a calculated field (that returns a Number) to do the work for us. The idea is to assign each possible condition with an integer value. The higher the number, the higher the sort order.
First, create a Calculated field. I called mine ‘Sort-Value’.
Next, write a formula to assign each condition a numerical value. I used a combination of ‘IF’ ‘AND’ operators. More information on how to use calculated formula operators can be found here.
Here’s a closer look:
You can now use the Calculated column to sort the list items. If anyone knows of a different way to accomplish this, please leave a comment below.
For example, say you have a list called ‘Defects’ that has the following two Choice fields: ‘Impact’ and ‘Status’.
Impact = {“Red, “Yellow”, “Green”}
Status = {“Open”, “Closed”}
The requirement is for the Impact field to be sorted in the following order: “Red”, “Yellow”, “Green”. All “Open” items should appear before the “Closed” items.
So what to do…?
We can use a calculated field (that returns a Number) to do the work for us. The idea is to assign each possible condition with an integer value. The higher the number, the higher the sort order.
First, create a Calculated field. I called mine ‘Sort-Value’.
Next, write a formula to assign each condition a numerical value. I used a combination of ‘IF’ ‘AND’ operators. More information on how to use calculated formula operators can be found here.
Here’s a closer look:
You can now use the Calculated column to sort the list items. If anyone knows of a different way to accomplish this, please leave a comment below.
Nice one, Steve!