sort
Description
Sorts a list.
list sort( list target [, mixed sortmethod ] )
Sorts a list. This function modifies the list that is passed. It will also return the list. If
sortmethod
is not passed, will sort a list of numbers or strings in ascending, case-sensitive order.
If sortmethod
is a string, will assume that target
is a list of objects. It will sort by the field in each object named sortmethod
.
If sortmethod
is a lambda, will use the two-parameter lambda to compare each element, where for elements a
and b
, the lambda should return a number less than 0 ifa < b
, a number greater than 0 if a > b
, and 0 if a == b
.
Note: Calling sort()
anywhere in the template will sort the entire content array, regardless if it’s assigned to a specified variable.
Examples
Simple sort:
{sort([5, 3, 12, 4, 1]) ---> [1, 3, 4, 5, 12] {sort(['gamma', 'beta', 'delta', 'alpha']) ---> [alpha, beta, delta, gamma]
Passing a string field name:
{content = [ {'title':'Product A','order':3}, {'title':'Product B','order':4}, {'title':'Product C','order':2}, {'title':'Product D','order':1}]} {sort(content, 'order')} {content[0].title} = Product D {content[1].title} = Product C {content[2].title} = Product A {content[3].title} = Product B
Reverse order (of the above example):
{content = [ {'title':'Product A','order':3}, {'title':'Product B','order':4}, {'title':'Product C','order':2}, {'title':'Product D','order':1}]} {sort(content, '-order')} {content[0].title} = Product B {content[1].title} = Product A {content[2].title} = Product C {content[3].title} = Product D
Sort and Display Content by Date
Use Case: If you have items in a feed and are passing a “date” parameter and wish to sort content from oldest to newest.Zephyr:
{content = sort(content, 'date')} {foreach content as c} <a href={c.url}>{c.title}</a><br/> {/foreach}
Output: Oldest Item Second Oldest Item Newest Item Using our feed example: From the Fashion Blog: The Most Comfortable Boots You’ll Ever Own Spider-Man: Threat or Menace? Alexander Harris elected to Los Angeles House of Representatives Get Your First Look at the New iPhone at the Times Square Apple Store Stephen King’s New Book is a Real Scream!
Explanation: This script uses the sort() function to rearrange objects by a particular method. In this instance, it’s rearranging content in a data feed by date, printing out the content in order from oldest to newest based on a “date” parameter.
Sort and Display Content by Price
Use Case: If you have items in a feed and are passing a “price” parameter and wish to sort content from highest priced to lowest priced. Note the “-” sign is used to reverse the order (i.e. go from highest to lowest as opposed to lowest to highest).
Zephyr:
{content = sort(content, '-price')}
{foreach content as c}
<a href={c.url}>{c.title}</a>
<br/>
{/foreach}
Output: Most Expensive Item
Second Most Expensive Item Least Expensive Item Using our feed example: Women’s Leather Jacket
Men’s Chelsea Boots To Kill a Mockingbird Salem’s Lot Invisible Monsters: A Novel From the Fashion Blog: The Most Comfortable Socks You’ll Ever Own
Explanation: This script uses the sort() function to rearrange objects by a particular method. In this instance, it’s rearranging content in a data feed by “reverse” weight (note the “-” before “weight), printing out the content in order from highest priced to lowest priced based on a “price” parameter.