How to group data by first letter and order it alphabetically from a .csv file in liquid?

a 夏天 提交于 2019-11-29 16:22:11

Well, you need to check somewhere whether the first letter of the current term differs from the first letter of the previous term.

Try this:

{% assign lastletter = "" %}

{% for listing in site.data.terminology %}

    {% assign tmpletter = listing.term | slice: 0 | upcase %}

    {% if tmpletter != lastletter %}
        <h1>{{ tmpletter }}</h1>
    {% endif %}

    {{ listing.term }}: {{ listing.definition }}<br>

    {% assign lastletter = tmpletter %}

{% endfor %}

I just save the first letter of the current term to the variable tmpletter.
(slice truncates the string and upcase converts it to uppercase because I want to display it in uppercase)

Then, if it's different from the first letter of the last term, I display it.

HTML output:

<h1>A</h1>
again: now it is here<br>
aunt: another explanation for two<br>
<h1>B</h1>
borrow: something from someone<br>
brother: new explanation for one<br>
<h1>F</h1>
father: this is it<br>
forbidden: fruit<br>
<h1>U</h1>
uncle: and last one for three, with the use of comma fin<br>
utah: this is a state<br>

(I didn't bother with getting the <ul><li> stuff right, you'll probably get this to work by yourself)


the code works well but only if the csv file is already ordered alphabetically, I need the code to do both the ordering and the adding of the first letter.

Okay, then you need to combine your code and my code, i.e. you need to apply my "determine whether the first letter changed" part to your {% for allterms in allsortedlistings %} part.

Something like this:

{% capture thelistings %}
  {% for listing in site.data.terminology %}
    {{ listing.term }}: {{ listing.definition }}
  {% endfor %}
{% endcapture %}
{% assign allsortedlistings = thelistings | split:"   " | sort %}

{% assign lastletter = "" %}

    <ul>
{% for allterms in allsortedlistings %}

    {% assign tmpletter = allterms | strip | slice: 0 | upcase %}

    {% if tmpletter != lastletter %}
        <li>{{ tmpletter }}</li>
    {% endif %}

        <li>{{ allterms }}</li>

    {% assign lastletter = tmpletter %}

{% endfor %}
    </ul>

This is not the 100% finished solution, you still need to figure out how to end/start the <ul> sections when the first letter changes.

Plus, there's an empty <li> at the beginning, probably because you're splitting by empty strings and thelistings contains a lot of empty strings (because of the line breaks and indentations inside {% capture thelistings %}...{% endcapture %}.

(for the same reason, I needed to strip all blanks from the string in the {% assign tmpletter = ... line, to find the first character that isn't a blank)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!