Sorting items that don't have a 'sort_by' attribute

| 5 Comments | No TrackBacks |
Sometimes you just want to sort some items according to some ranking, but there is no convenient 'sort_by' attribute available in Movable Type's templating language.  For example, assume you want to sort a list of entries by the number of comments on them.  Or a list of authors by number of posted entries.  Or a list of categories by some value stored in a custom field.  Or...
For cases like this, the <mt:setvarblock> tag in combination with the <mt:loop> tag can do miracles.  Let's look at an example:

<mt:entries lastn="999">
<mt:setvarblock name="theentry">
<li><href="mt:entrypermalink"><mt:entrytitle> (<mt:entrycommentcount>)</a>
</mt:setvarblock>
<mt:setvarblock name="theentries{$theentry}"><mt:entrycommentcount></mt:setvarblock>
</mt:entries>

Basically this creates an array ("theentries", with links to the various entries as keys, and the number of comments as values.  Now to display this array, we can use a loop tag:

<mt:loop name="theentries" sort_by="value numeric reverse">
<mt:var name="__key__">
</mt:loop>​

Note how the array is sorted by value instead of by key (which is more common).  But in this case, we have to store the links as the keys and the counts as values since the links are unique while the counts may not be.

The result of this is a neatly sorted list of entries, along with their comment counts:


(Yes, not a lot of comments yet but this article was written when this blog was still young)

When your blog grows, you might want to modify the code for the loop a bit, so the output is limited to just the top n entries:

<mt:loop name="theentries" sort_by="value numeric reverse">
<mt:if name="__counter__" lt="4"><mt:var name="__key__"></mt:if>
</mt:loop>​

This loop will just display the three entries with the most comments, using the built-in "__counter__" variable that is available inside each loop tag.

TIP: use a smaller value for the 'lastn' attribute of the initial <mt:entries> loop if you want to avoid always having the same old entries (with lots of comments) at the top of your list.

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/8

5 Comments

Excellent post. I was checking constantly this blog and I am impressed!
Very useful information specially the last part :) I care for such information a lot.
I was seeking this certain info for a long time.

Thank you and good luck.

Excellent post. I was checking constantly this blog and I am impressed!
Very useful information specially the last part :) I care for such information a lot.
I was seeking this certain info for a long time.

Thank you and good luck.

I am regular reader, how are you everybody? This piece of writing posted at this website is
truly good.

I am regular reader, how are you everybody? This piece of writing posted at this website is
truly good.

I really love your blog.. Excellent colors & theme.

Did you build this web site yourself? Please reply back
as I'm hoping to create my own personal website and would love to learn where you got this from or just what the theme is called. Appreciate it!

Leave a comment

Sign in to comment, or comment anonymously.