Forums / Setup & design / Sorting Based on Attribute Content

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}

:)

eZ debug

Timing: Jan 31 2025 06:00:47
Script start
Timing: Jan 31 2025 06:00:47
Module start 'content'
Timing: Jan 31 2025 06:00:47
Module end 'content'
Timing: Jan 31 2025 06:00:47
Script end

Main resources:

Total runtime0.2147 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0052 587.9531180.8281
Module start 'content' 0.00520.0073 768.781393.9141
Module end 'content' 0.01250.2021 862.6953523.8281
Script end 0.2146  1,386.5234 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00321.4811200.0002
Check MTime0.00120.5812200.0001
Mysql Total
Database connection0.00060.276710.0006
Mysqli_queries0.161175.05731410.0011
Looping result0.00120.54131390.0000
Template Total0.201693.910.2016
Template load0.00070.335010.0007
Template processing0.200993.585410.2009
Override
Cache load0.00050.226510.0005
Sytem overhead
Fetch class attribute can translate value0.00090.430510.0009
XML
Image XML parsing0.00020.104010.0002
General
dbfile0.01436.6840200.0007
String conversion0.00000.002930.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs