[英]Make array elements lowercase with Liquid filters for sorting
I can't figure out how case-insesitively to sort an array containing strings: ["A", "C", "E", "b", "d"]
into ["A", "b", "C", "d", "E"]
.我不知道如何不区分大小写地对包含字符串的数组进行排序:
["A", "C", "E", "b", "d"]
到["A", "b", "C", "d", "E"]
。
{% assign input = "A,C,E,b,d" | split:"," %}
{{ input | join: "-" }}
{{ input | map: 'downcase' | join: "-" }}
{{ input | map: 'downcase' | sort | join: "-" }}
{{ input | map: 'length' | join: "-" }}
{{ input | map: 'size' | join: "-" }}
What am I missing about map:
?我对
map:
缺少什么map:
?
Expected output:预期输出:
A-C-E-b-d
a-c-e-b-d
a-b-c-d-e
1-1-1-1-1
1-1-1-1-1
Actual output:实际输出:
A-C-E-b-d
----
----
----
----
Note: at first I tried map: downcase
(without quotes), but got no implicit conversion from nil to integer .注意:起初我尝试了
map: downcase
(不带引号),但没有从 nil 到 integer 的隐式转换。
sort_natural
was added after I asked the question.我提出问题后添加了
sort_natural
。 See other answer.见其他答案。 I'll leave this answer here, because it shows how can you do a sorting by any key.
我将把这个答案留在这里,因为它显示了如何按任意键进行排序。
{% assign input = "A,C,E,b,d" | split:"," %}
{% capture intermediate %}{% for entry in input %}{{ entry | downcase }}{{ entry }}{% unless forloop.last %}{% endunless %}{% endfor %}{% endcapture %}
{% assign intermediate_sorted = intermediate | split:'' | sort %}
{% capture sorted %}{% for entry in intermediate_sorted %}{{ entry | split: '' | last }}{% unless forloop.last %}{% endunless %}{% endfor %}{% endcapture %}
{% assign sorted = sorted | split: '' %}
{{ sorted | join: "-" }}
will output AbCdE
.将输出
AbCdE
。
The US ( Unit Separator , \ , not \␟ ) and RS ( Record Separator , \ , not \␞ ) are the two characters unlikely to appear in the input, so they can be used safely most of the time. US(单位分隔符, \\ u001F ,而不是\␟ )和RS(记录分隔符, \ ,而不是\␞ )是两个不太可能出现在输入中的字符,因此它们在大多数情况下都可以安全使用。 They could be
,
and |
他们可能是
,
和|
if you want to sort CSS IDs for example.例如,如果您想对 CSS ID 进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.