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"]
.
{% 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:
?
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 .
sort_natural
was added after I asked the question. 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
.
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. They could be ,
and |
if you want to sort CSS IDs for example.
The sort_natural
filter does a case-insensitive sort:
{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}
{{ my_array | sort_natural | join: ", " }}
Outputs giraffe, octopus, Sally Snake, zebra
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.