sort

Welcome to our newly overhauled Zephyr pages! See our new Overview, Examples, and Index.
Feedback is welcome at bottom of each page.

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 sortmethodis not passed, will sort a list of numbers or strings in ascending, case-sensitive order.

If sortmethodis a string, will assume that targetis a list of objects. It will sort by the field in each object named sortmethod.

If sortmethodis a lambda, will use the two-parameter lambda to compare each element, where for elements aand 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.

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.

Have a Dynamic Welcome Message Based on Last List Signup

Use Case: Within a Welcome email, you want to display the name of the Natural List a user most recently signed up to. Using push() in conjunction with sort(), you can display the name of the most recent Natural List a user was added to.

Zephyr:

{lists = []}
{foreach profile.lists_signup as k,v}
{push("lists",{"name":k,"time":v})}
{/foreach}
{listName = first(sort(lists,'-time')).name}
Thank you for signing up to our {listName} list!

Output: Thank you for signing up to our Daily Newsletter list!

Explanation: This script loops through the “lists_signup” parameter on each user’s “profile object,” assigning two temporary values: “k” (the key, in this instance, the list name) and “v” (the value, in this instance, the list signup time). We then use the push() function to add those values as “name” and “list” into an array called “lists.” Next, a a local variable called “listName” is created, and for the value, the sort() function orders this array by newest to oldest list signup time (“-time”), and then isolates the first value, i.e. the latest list the user signed up to. The {listName} variable is then displayed in the welcome message in the template.

Top