如何按第一个字母分组数据,并从液体中的.csv文件按字母顺序排列? [英] How to group data by first letter and order it alphabetically from a .csv file in liquid?

查看:244
本文介绍了如何按第一个字母分组数据,并从液体中的.csv文件按字母顺序排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法按照字母顺序排列所有数据,但是我不能做的是按第一个字母分组,并显示如下的第一个字母:



A




  • 苹果

  • 箭头



B




  • 乐队

  • 模糊



等等...



这是我显示有序数据的代码。 p>

  --- 
layout:default
---
{%capture thelistings%}
{%在site.data.terminology中列出%}
< li> {{listing.term}}:{{listing.definition}}< / li>
{%endfor%}
{%endcapture%}
{%assign allsortedlistings = thelistings | split:| sort%}

< ul>
{allsortedlistings%allterms%}
{{allterms}}
{%endfor%}
< / ul>

这会输出:



    $ b $ > uncle:and last one for three,with the use of comma fin
  • utah:这是一个状态


是否需要检查当前词的第一个字母是否与上一个词的第一个字母不同

>

尝试此操作:

  {%assign lastletter =%} 

{%在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%}

我只是将当前期限的第一个字母保存到变量 tmpletter

=https://docs.shopify.com/themes/liquid/filters/string-filters#slice =nofollow> slice 截断字符串和 upcase 将其转换为大写,因为我要以大写显示)



然后,如果它不同于上一个词的第一个字母,显示它。



HTML输出:

 < h1> ; / h1> 
again:now it is here< br>
aunt:另一个解释两个< br>
< h1> B< / h1>
borrow:某人从某人< br>
brother:一个新解释< br>
< h1> F< / h1>
father:this is it< br>
forbidden:fruit< br>
< h1> U< / h1>
叔叔:和最后一个为三,使用逗号fin< br>
utah:这是一个状态< br>

;< li> 正确的东西,你可能会自己工作)







代码运行良好,但是如果csv文件已经按字母顺序排序,我需要代码来执行第一个字母的排序和添加。


好的,那么你需要将你的代码和我的代码合并,即你需要应用我的确定第一个字母是否改变部分到你的



这样的:

  {%capture thelistings%} 
{%在site.data.terminology%}中列出
{{listing.term}}:{{ list.definition}}
{%endfor%}
{%endcapture%}
{%assign allsortedlistings = thelistings | split:| sort%}

{%assign lastletter =%}

< ul>
{allsortedlistings%allterms%}

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

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

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

{%assign lastletter = tmpletter%}

{%endfor%}
< / ul>

这不是100%完成的解决方案,你仍然需要弄清楚如何结束/ < ul> 第一个字母更改时的部分。



< li> 在开头,可能是因为您要拆分空字符串, thelistings 包含很多空字符串(因为 {%capture thelistings%} ... {%endcapture%} 中的换行符和缩进。



(出于同样的原因,我需要 { {%assign tmpletter = ... 行中的字符串中的所有空格,不是空白的第一个字符)


I have managed to order all the data alphabetically but what I can't do is group it by first letter and also display the first letter like below:

A

  • apple
  • arrow

B

  • band
  • blur

and so on...

Here is my code for displaying the ordered data.

---
layout: default
---
{% capture thelistings %}
  {% for listing in site.data.terminology %}
    <li>{{ listing.term }}: {{ listing.definition }}</li>
  {% endfor %}
{% endcapture %}
{% assign allsortedlistings = thelistings | split:"   " | sort %}

    <ul>
{% for allterms in allsortedlistings %}
        {{ allterms }}
{% endfor %}
    </ul>

This outputs:

  • again: now it is here
  • aunt: another explanation for two
  • borrow: something from someone
  • brother: new explanation for one
  • father: this is it
  • forbidden: fruit
  • uncle: and last one for three, with the use of comma fin
  • utah: this is a state

解决方案

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)

这篇关于如何按第一个字母分组数据,并从液体中的.csv文件按字母顺序排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆