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
-
The Interchange database identifier. This must be supplied and should normally be 'products' if using the default name for the database.
field
-
The field (column) of the database to be sorted on.
option
-
None, any, or combinations of the options:
f case-insensitive sort (folded) (mutually exclusive of n) n numeric order (mutually exclusive of f) r reverse sort
-n
-
The starting point of the list to be displayed, beginning at 1 for the first entry.
+n
-
The number of entries to display in this list segment.
=n-n
-
The starting and ending point of the list display. This is an alternative to -n and +n. They should be specified in only one form. If both are specified, the last one will take effect.
...
-
Don't really put ... in. This means that many sort levels are specified. Lots of sort levels with large databases will be quite slow.
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]
-
Will display:
34-101 Family Portrait 00-0011 Mona Lisa 19-202 Radioactive Cats 99-102 The Art Store T-Shirt
-
\Alternatively:
[loop 00-0011 19-202 34-101 99-102] [sort products:title -3 +2] [loop-code] [loop-field title]<BR> [/loop]
-
\Displays:
19-202 Radioactive Cats 99-102 The Art Store T-Shirt
-
The tag [sort products:title =3-4] is equivalent to the above.
Search list
-
A search of all products (i.e., http://yoursystem.com/cgi-bin/simple/scan/ra=yes):
[search-list] [sort products:artist products:title:rf] [item-field artist] [item-field title]<BR> [/search-list]
-
will display:
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
-
Note the reversed order of the title for Van Gogh and the presence of the accessory item Gilded Frame at the front of the list. It has no artist field and, as such, sorts first).
Adding a slice option:
[search-list] [sort products:artist products:title:rf =6-10] [item-field artist] [item-field title]<BR> [/search-list]
-
will display:
Sandy Skoglund Radioactive Cats The Art Store The Art Store T-Shirt Vincent Van Gogh The Starry Night Vincent Van Gogh Sunflowers
-
If the end value/chunk size exceeds the size of the list, only the elements that exist will be displayed, starting from the start value.
Shopping cart
[item-list] [sort products:price:rn] [item-price] [item-code]<BR> [/item-list]
-
will display the items in the shopping cart sorted on their price, with the most expensive shown first. NOTE: This is based on the database field and doesn't take quantity price breaks or discounts into effect. Modifier values or quantities cannot be sorted.
Complete database contents
[tag each products] [sort products:category products:title] [loop-field category] [loop-field title] <BR> [/tag]
-
A two level sort that will sort products based first on their category, then on their title within the category.
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.