6. Sorting

Interchange has standard sorting options for sorting the search lists, loop lists, and item lists based on the contents of database fields. In addition, it adds list slices for limiting the displayed entries based on a start value and chunk size (or start and end value, from which a chunk size is determined). All accept a standard format sort tag which must be directly after the list call:

   [loop 4 3 2 1]
   [sort -2 +2]
       [loop-code]
   [/loop]

   [search-list]
   [sort products:category:f]
       [item-price] [item-description]<BR>
   [/search-list]

   [item-list]
   [sort products:price:rn]
       [item-price] [item-code]<BR>
   [/item-list]

   [loop search="ra=yes"]
   [sort products:category products:title]
   [loop-field category] [loop-field title] <BR>
   [/loop]

All sort situations, [search list], [loop list], [tag each table], and [item-list], take options of the form:

 [sort database:field:option* -n +n =n-n ... ]

database

field

option

          f   case-insensitive sort (folded) (mutually exclusive of n)
          n   numeric order (mutually exclusive of f)
          r   reverse sort

-n

+n

=n-n

...

Multiple levels of sort are supported, and database boundaries on different sort levels can be crossed. Cross-database sorts on the same level are not supported. If using multiple product databases, they must be sorted with embedded Perl. This is actually a feature in some cases, all items in a used database can be displayed before or after new ones in products.

Examples, all based on the simple demo:

Loop list

            [loop 00-0011 19-202 34-101 99-102]
            [sort products:title]
                [loop-code] [loop-field title]<BR>
            [/loop]
            34-101 Family Portrait
            00-0011 Mona Lisa
            19-202 Radioactive Cats
            99-102 The Art Store T-Shirt
            [loop 00-0011 19-202 34-101 99-102]
            [sort products:title -3 +2]
                [loop-code] [loop-field title]<BR>
            [/loop]
            19-202 Radioactive Cats
            99-102 The Art Store T-Shirt

Search list

            [search-list]
            [sort products:artist products:title:rf]
                [item-field artist] [item-field title]<BR>
            [/search-list]
            Gilded Frame
            Grant Wood American Gothic
            Jean Langan Family Portrait
            Leonardo Da Vinci Mona Lisa
            Salvador Dali Persistence of Memory
            Sandy Skoglund Radioactive Cats
            The Art Store The Art Store T-Shirt
            Vincent Van Gogh The Starry Night
            Vincent Van Gogh Sunflowers
            [search-list]
            [sort products:artist products:title:rf =6-10]
                [item-field artist] [item-field title]<BR>
            [/search-list]
            Sandy Skoglund Radioactive Cats
            The Art Store The Art Store T-Shirt
            Vincent Van Gogh The Starry Night
            Vincent Van Gogh Sunflowers

Shopping cart

            [item-list]
            [sort products:price:rn]
                [item-price] [item-code]<BR>
            [/item-list]

Complete database contents

            [tag each products]
            [sort products:category products:title]
            [loop-field category] [loop-field title] <BR>
            [/tag]

Note that large lists may take some time to sort. If a product database contains many thousands of items, using the [tag each products] sort is not recommended unless planning on caching or statically building pages.