Monday, December 24, 2007

How To Sort The Labels Widget Reverse Alphabetically

Someone on the Blogger Help Group asked if it was possible to sort the Labels widget z to a rather than a to z, someone replied, "no". Of course, with JavaScript the answer is actually "yes".

You can see an example of this widget in the Compender Sandbox. Here's the code:
<b:widget id='ReverseLabels' locked='false' title='Reverse Alphabetical Labels' type='Label'>
<b:includable id='main'>
<b:if cond='data:title'>
<h2><data:title/></h2>
</b:if>
<div class='widget-content'>
<ul>
<script type="text/javascript">
/*
Reverse Alphabetical Tag Sort
by Raymond May Jr.
http://www.compender.com
Released to the Public Domain
*/
var root = "<data:blog.homepageUrl/>";
var tagUrl = "search/label/";
var labels = new Array();

<b:loop values='data:labels' var='label'>
labels.push("<data:label.name/>");
</b:loop>

i=labels.length;
while(i > 0)
{
i=i-1;
document.write("<li><a href='"+root+tagUrl+labels[i]+"'>"+labels[i]+"</a></li>");
}
</script>
</ul>
<b:include name='quickedit'/>
</div>
</b:includable>
</b:widget>