Sorting by second element of split array in liquid - sorting

I am new to Liquid, so forgive me if this is obvious.
I have some data whose titles are prefixed with A1, B10, C64, etc. before the real text I want to sort on, for example, "A1 Something" "B10 Nothing" "A23 Another".
If I use
{% assign itemssorted = product.ds_related_products | sort: 'title' %}
{% for item in itemssorted %}
then I get the items sorted according to the prefixes, whereas what I want is the items sorted by the text after the prefixes.
Is there something like
{% assign itemssorted = product.ds_related_products | sort: 'title' | split: ' ' .... %}
I can use to implement this?

Try to change the sort:price for the variable you want to sort:
{% assign products = collection.products | sort: 'price' %}
{% for product in products %}
<h4>{{ product.title }}</h4>
{% endfor %}
<!-- products = "a", "b", "A", "B" -->
{% assign products = collection.products | sort: 'title' %}
{% for product in products %}
{{ product.title }}
{% endfor %}

Related

Dynamic lists from Jekyll YAML arrays

I have a list of products, each with a list of features of nested yaml lists
- product: Product n1
features:
- feature1
- feature2
- feature3
- product: Product n2
features:
- feature1
- feature2
- product: Product n3
features:
- feature1
- feature2
- feature3
- feature4
- feature5
Goal: create a list of divs, one for each product, with a title and the list of features.
I tried using for subitem in item but I can't get it to work - any ideas?
I solved the problem with the following code:
{% for product in site.data.products %}
<div>
<h2>{{ product.product }}</h2>
<ul>
{% for feature in product.features %}
<li>{{ feature }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}

Sum of multiple table row

How can i get sum of all these counts?
{{ DB::table('A')->count()}}
{{ DB::table('B')->count()}}
{{ DB::table('C')->count()}}
{{ DB::table('D')->count()}}
// Your Tables Which You Know Them Counts
$tables = ['A', 'B'];
// Our Count Start From Zero
$count = 0;
// Count Each Table And Add Our Count
foreach ($tables as $table) {
$tableCount = DB::table($table)->count();
$count += $tableCount;
}
// Display Count With dd()
dd( $count );
// Or Display In View
return view('index', compact('count'));
Like this?:
{{ $a = DB::table('A')->count()}}
{{ $b = DB::table('B')->count()}}
{{ $c = DB::table('C')->count()}}
{{ $d = DB::table('D')->count()}}
{{ $sum = $a + $b + $c + $d }}
But you may want to send all these data from a controller method instead of doing it in a view.

Laravel Blade - Header & Footer in a Loop

I have data like this sorted by Category:
Category 1, Product 1, Amount
Category 1, Product 2, Amount
Category 2, Product 3, Amount
I like to print it using blade as
Category 1
Product 1 Amount
Product 2 Amount
....
Total Amount of Category 1 : xxxxx
Category 2
Product 3 Amount
....
Total Amount of Category 2 : xxxxx
I am assuming that $categories is the sorted multidimensional array with indexes category,product and amount.
#for ($i = 0; $i < count($categories) ; $i++)
<?php $category_product_toatl=0; ?>
{{ $categories[$i]['category'] }}
{{ $categories[$i]['product'] }} {{ $categories[$i]['amount']}}
<?php
$category_product_total= $category_product_total+ $categories[$i]['amount'];
?>
#for ($j = $i+1; $j < count($categories) ; $j++)
#if($categories[$i]['category']== $categories[$j]['category'])
{{ $categories[$j]['product'] }} {{ $categories[$j]['amount']}}
<?php
$category_product_total= $category_product_total+ $categories[$j]['amount'];
?>
#else
break;
#endif
#endfor
Total Amount of {{ $category[$i] }} : {{ $category_product_total }}
#endfor

Sort Shopify Products by Multiple Filters

I am trying to sort a collection of products by more than one filter. I want to sort my products by their tag ( collection.products | sort: 'tags' ) and by title ascending ( collection.products | sort: 'title' ). I have tried multiple variations of the following:
{% assign sortedProducts = collection.products | sort: 'title' | sort: 'tags' %}
{% assign sortedProducts = collection.products | sort: 'tags' | sort: 'title' %}
{% assign sortedProducts = collection.products | sort: 'title' %}
{% assign sortedProducts = sortedProducts | sort: 'tags' %}
{% assign sortedProducts = collection.products | sort: 'tags' %}
{% assign sortedProducts = sortedProducts | sort: 'title' %}
None of them work. I have seen questions with answers that involve filtering by tag and then sorting alphabetically, but I was hoping to sort by tag and title on the same page. If it's not possible, then I'll look more into filtering by tag before sorting.
Thanks!
Apparently with liquid tags you shouldn't leave spaces, like below(untested):
{% assign sortedProducts = collection.products|sort:'title'|sort:'tags' %}
{% assign sortedProducts = collection.products|sort:'tags'|sort:'title' %}
{% assign sortedProducts = collection.products|sort:'title' %}
{% assign sortedProducts = sortedProducts|sort:'tags' %}
{% assign sortedProducts = collection.products|sort:'tags' %}
{% assign sortedProducts = sortedProducts|sort:'title' %}
Then there's one more complication: pagination.
There is a pagination setting somewhere in your settings or theme settings. The default is 50 products per 'page'. The problem in my experience is when you ask it to sort it may only sorting within the first 50 products :(

Cannot group by custom jekyll collection

I'm trying to group elements from a custom jekyll collection by the value of one of their metadata (date).
So, I'm doing {% for elt in site.my_collection | group_by: "date" %} but it loops through the collection normally, just like I've written {% for elt in site.my_collection %}.
Even stranger is if I write in my template {{ site.my_collection | group_by: "date" }}, then it displays correctly the grouped collection [{"name" => "day1", "items" => [#, #, #]}, {"name" => "day2", "items" => [#]}].
What am I doing wrong? Is this because I'm using a custom collection?
Thanks.
You cannot loop and sort/group at the same time.
You have to assign and sort/group then loop
That's true for Jekyll elements like pages, posts or collection.
{% assign collection = site.my_collection | group_by: "date" %}
{% for group in collection %}
<h3>{{ group.name | date: "%-d %B %Y" }}</h3>
<ul>
{% for item in group.items %}
<li>{{item.data}}</li>
{%endfor%}
</ul>
{%endfor%}

Resources