Skip to content

Instantly share code, notes, and snippets.

@sskylar
Last active Jan 26, 2021
Embed
What would you like to do?
Sort Jekyll tags by popularity (number of posts)
<ul>
{% capture tags %}
{% for tag in site.tags %}
<li data-sort="{{ site.posts.size | minus: tag[1].size | prepend: '0000' | slice: -4, 4 }}">
<a href="/{{ site.tag_page_dir }}/{{ tag[0] | slugify: 'pretty' }}">{{ tag[0] }} <span>{{ tag[1].size }}</span></a>
</li>
{% endfor %}
{% endcapture %}
{{ tags | split:'</li>' | sort | join:'</li>' }}
</ul>
@sskylar
Copy link
Author

sskylar commented Oct 19, 2016

Outputs something like:

<ul>
  <li data-sort="0000"><a href="/tags/tomato">Tomato <span>100</span></a></li>
  <li data-sort="0080"><a href="/tags/kale">Kale <span>20</span></a></li>
  <li data-sort="0099"><a href="/tags/arugula">Arugula <span>1</span></a></li>
  <li data-sort="0099"><a href="/tags/bok-choy">Bok choy <span>1</span></a></li>
  <li data-sort="0099"><a href="/tags/zucchini">Zucchini <span>1</span></a></li>
</ul>

Tags are sorted descending based on the number of posts, included within a <span> tag. Any tags with the same number of posts are sorted alphabetically.

@Foorack
Copy link

Foorack commented Jan 1, 2017

Thanks! 😃

@jindaxia
Copy link

jindaxia commented Dec 29, 2018

My result is

<ul>
  </li>
  <li data-sort="0000"><a href="/tags/tomato">Tomato <span>100</span></a></li>
  <li data-sort="0080"><a href="/tags/kale">Kale <span>20</span></a></li>
  <li data-sort="0099"><a href="/tags/arugula">Arugula <span>1</span></a></li>
  <li data-sort="0099"><a href="/tags/bok-choy">Bok choy <span>1</span></a></li>
  <li data-sort="0099"><a href="/tags/zucchini">Zucchini <span>1</span></a>
</ul>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment