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 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
Top