Forums / Setup & design / Sorting Based on Attribute Content

"Please Note:
  • At the specific request of Ibexa we are changing this projects name to "Exponential" or "Exponential (CMS)" effective as of August, 11th 2025.
  • This project is not associated with the original eZ Publish software or its original developer, eZ Systems or Ibexa".

Sorting Based on Attribute Content

Author Message

Betsy Gamrat

Friday 26 January 2007 8:19:11 am

Hi,

If you need to sort content based on attributes, you can use this approach:

First, install the <b>sort template plugins</b>. I used them on eZ 3.8.6 and it was flawless (my thanks to the author).

http://ez.no/community/contribs/template_plugins/arraysortoperator

Once the extension is installed, you can use it like so:

{def $sorted_month_array=array()}
{foreach $months as $k => $v}
  {set $sorted_month_array=$sorted_month_array|append(concat($v.data_map.month.value.0,'-',$k))}
{/foreach}
{set $sorted_month_array=$sorted_month_array|sort('numeric')}

In this case, I created an array of the attribute value (<i>$v.data_map.month.value.0</i>), a dash (<i>-</i>) that serves as a separator, and the array index (<i>$k</i>). Then, the <b>sort</b> template operator was used, with the numeric option - to ensure 11 came after 2.

To access the array, you loop through the elements, exploding out the data from the strings and using it as an index into the array.

{* Loop through the sorted array and if the month is supposed to be displayed in the slot - display it *}

{def $bits=''}
{def $m=0}
{for 1 to 12 as $i}
  <td>
    {set $bits=$sorted_month_array[$m]|explode('-')}
    {if $bits.0|eq($i)}
      {node_view_gui content_node=$months[$bits.1] view='line' format='cell'}
      {set $m=$m|inc}
    {else}
      &nbsp;
    {/if}
  </td>
{/for}

In this case, the months were being displayed in a table, in a row of 12 cells. I wanted each month to go into the proper cell (January would be first, February second). The array did not necessarily have all 12 months - so the loop creates the 12 cells and decides whether to put the content in based on the content.

<b>$bits</b> is the cross-reference. The first number is the month, the second is the position in the array (fetched).

Hope this helps.

Betsy Gamrat

Monday 02 April 2007 8:11:01 pm

As often happens - another approach appears. This one builds a sorted list, rather than sorting it after assembly.

  {def $sorted_event_array=array() $value='' $i=0}
  {foreach $events as $k => $v}
    {set $value=concat($v.data_map.start_date.content.day,'-',$k)}
    {set $i=0}
    {while and(is_set($sorted_event_array[$i]),$value|gt($sorted_event_array[$i]))}
        {set $i=$i|inc}
    {/while}
    {set $sorted_event_array=$sorted_event_array|insert($i,$value)}
  {/foreach}

:)