2 Replies Latest reply on Apr 17, 2017 5:50 AM by LarrySawyer27

    Liquid Sorting workaround - strange quirk

    LarrySawyer27 Level 1

      Basically, we have a Food web app, which contains all of the restaurant's menu items. Each menu item can appear in any or all of these sections—brunch, lunch, supper, kids, and catering—and has separate price and description fields for each. They also have separate "number" fields for each, which is basically weighting. An item with a "Lunch Number" of 1 should show up first in its section.

       

      Here's the code that makes that happen, a combination of ideas from other forum posts...

                          {module_webapps id="23256" filter="classified" itemId="113803" template="" collection="appetizers"}
                          {% capture appetizersData -%}
                              {% for i in appetizers.items -%}
                                  {% if i.["supper number"] != "" -%}
                                  {{i.["supper number"]}}-{{i.itemid}},
                              {% else -%}
                                  {{i.name | downcase | remove: 'the ' | truncatewords: 1,''}}-{{i.itemid}},
                              {% endif -%}
                              {% endfor -%}
                          {% endcapture -%}
                         
                          {% assign appetizersArray = appetizersData | split: ',' | sort %}
                          {% for i in appetizersArray -%}
                              {% assign appetizersId = i | split: '-' %}
                              {% for d in appetizersId offset:1 %}
                              {module_webapps id="23256" filter="item" itemId="{{d}}" template="/Layouts/WebApps/Food/supper.tpl"}
                              {% endfor -%}
                          {% endfor -%}

      I'm sure it's not the most efficient to call each item in a loop like this, but it gets the custom sorting job done for this site.

       

      The quirk that's happening is that most of the sections that use this are taking the first item alphabetically and sticking at the top, and then sorting the rest by the custom field. For example, on this page (https://cafemiranda.worldsecuresystems.com/menu/supper), under Appetizers, 50 MPH Tomatoes should be 12th in the list, but it's coming up first because it's the first one alphabetically. The items under Fries with That? are working fine, using the same liquid code.

       

      That was kind of a lot, but any thoughts?

        • 1. Re: Liquid Sorting workaround - strange quirk
          Liam Dilley Adobe Community Professional & MVP

          - Liquid supports sorting in Arrays.

          - BC does not support sorting on collections.

          - You do not need to do the above.

           

          Step one - pass a collection to a variable.

           

          {% myarray =  appetizers.items -%}

          {% myarray =  myarray | sort: 'mycustomfield' -%}

           

          Done

          1 person found this helpful
          • 2. Re: Liquid Sorting workaround - strange quirk
            LarrySawyer27 Level 1

            Wow, so much simpler, and it works perfectly! 17 lines down to 8, and this will simplify things on a number of our sites. Thank you!

             

            For posterity, here's what I ended up with:

             

                                {module_webapps id="23256" filter="classified" itemId="113803" template="" collection="appetizers"}

                                {% assign appetizersArray =  appetizers.items -%}

                                {% assign appetizersArray =  appetizersArray | sort: 'supper number' -%}

                                {% for i in appetizersArray -%}

                                <li>

                                    <h4 class="item-name">{{i.name}}</h4>

                                    <p class="item-desc">{{i.["supper description"]}} <span class="item-price">{{i.["supper price"]}}</span></p>

                                </li>

                                {% endfor -%}

            1 person found this helpful