TWiStErRob
TWiStErRob

Reputation: 46480

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"].

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

Upvotes: 0

Views: 610

Answers (2)

TWiStErRob
TWiStErRob

Reputation: 46480

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 A-b-C-d-E.

The US (Unit Separator, \u001F, not \u241F) and RS (Record Separator, \u001E, not \u241E) 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.

Upvotes: 3

Jonathan_W
Jonathan_W

Reputation: 648

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

Upvotes: 1

Related Questions