12.5. Sorting the product list
The products listed on your welcome page are shown in the same order that you entered them into products/products.txt. As you add more products, you will want this list to show up in a predictable order. To do this, you need to change the search parameters in index.html, which were originally:
[loop search="ra=yes/fi=products"]
You will recall that 'ra' stands for 'return all' and 'fi' stands for file. Let's add the search parameter 'tf', which specifies the sort field. You can specify the field either by name or by number (starting with 0), with names and order as given in the first line of products/products.txt). Make the following change in index.html:
[loop search="ra=yes/fi=products/tf=price"]
Refresh your browser. The default ordering is done on a character-by-character basis, but we were looking to do a numeric sort. For this you need to set 'to', the sort order, to 'n', for numeric:
[loop search="ra=yes/fi=products/tf=price/to=n"]
Refresh your browser. Now try reversing the sort order by adding 'r' to the 'to' setting:
[loop search="ra=yes/fi=products/tf=2/to=nr"]
You'll notice that it worked equally well to specify the sort field by number instead of name. You could also do a reverse alphabetical sort by description:
[loop search="ra=yes/fi=products/tf=1/to=r"]
Now let's try narrowing the search down a bit. Instead of returning all, we'll give 'se', the search paramerter, and and use 'su', which allows substring matches. To search only for products that have the word "test" in one of their fields, and sort the results by description, type:
[loop search="se=test/su=yes/fi=products/tf=description"]
Which seems like something that would be better done in a search box for your store visitors.
Before moving on, change this search back to the simple list, sorted by description:
[loop search="ra=yes/fi=products/tf=description"]