Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to alphabetically sort Shopify articles
<ul id="artists" class="medium-block-grid-4">
{% for artist in blog.articles %}
{% capture artist-name %}{{ artist.title | downcase }}{% endcapture %}
<li class="{{artist-name}}">
<a href="{{ artist.url }}">
<div class="artist-excerpt">{{ artist.excerpt }}</div>
<div class="artist-name">{{ artist.title }}</div>
<div class="country">{{ artist.metafields.global.country }}</div>
</a>
</li>
{% endfor %}
</ul>
<script>
function sortArtists(id) {
function getLastName(artistElement) {
return artistElement.className.split(' ').splice(-1,1);
}
var dataTree = $('#' + id);
var artists = dataTree.children('li').get();
artists.sort(function(a, b) {
var lastNameA = getLastName(a);
var lastNameB = getLastName(b);
return (lastNameA < lastNameB) ? -1 : 1;
});
$(artists).appendTo(dataTree);
}
sortArtists("artists");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.