【问题标题】:How to group data by first letter and order it alphabetically from a .csv file in liquid?如何按第一个字母对数据进行分组并按字母顺序从液体中的 .csv 文件中排序?
【发布时间】:2016-06-12 22:51:43
【问题描述】:

我已经设法按字母顺序对所有数据进行了排序,但我不能按首字母对其进行分组,并如下所示显示首字母:

一个

  • 苹果
  • 箭头

B

  • 乐队
  • 模糊

等等……

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

---
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>

这个输出:

  • 再次:现在它就在这里
  • 阿姨:两个的另一种解释
  • 借:某人的东西
  • 兄弟:一个新的解释
  • 父亲:就是这个
  • 禁止:水果
  • 大叔:最后一换三,用逗号分隔
  • 犹他州:这是一个州

【问题讨论】:

    标签: csv jekyll liquid alphabetical-sort


    【解决方案1】:

    嗯,你需要检查某处当前词条的首字母是否与上一个词条的首字母不同。

    试试这个:

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

    我只是将当前术语的第一个字母保存到变量tmpletter
    slice 截断字符串,upcase 将其转换为大写,因为我想以大写显示)

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

    HTML 输出:

    <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>
    

    (我没有费心把 &lt;ul&gt;&lt;li&gt; 的东西弄好,你可能会自己解决这个问题)


    代码运行良好,但仅当 csv 文件已按字母顺序排序时,我需要代码来执行排序和添加第一个字母。

    好的,那么您需要将您的代码和我的代码结合起来,即您需要将我的“确定首字母是否更改”部分应用到您的 {% for allterms in allsortedlistings %} 部分。

    类似这样的:

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

    这不是 100% 完成的解决方案,您仍然需要弄清楚当第一个字母发生变化时如何结束/开始 &lt;ul&gt; 部分。

    另外,开头有一个空的&lt;li&gt;,可能是因为你用空字符串分割,thelistings 包含 lot 的空字符串(因为里面有换行符和缩进{% capture thelistings %}...{% endcapture %}.

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

    【讨论】:

    • 代码运行良好,但仅当 csv 文件已按字母顺序排序时,我需要代码来执行排序和添加第一个字母。
    • 非常感谢您的帮助。我已经成功实现了显示无序列表的代码,我现在担心的是语义,在我的情况下,我绝对应该使用定义列表'
      '来显示配对,但是列表的顺序很重要并且这表示有序列表“
        ”。如果您有任何建议,请分享,尽管这是一个完全不同的主题。
    猜你喜欢
    • 2017-02-28
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2012-02-18
    • 1970-01-01
    相关资源
    最近更新 更多