sort
sort(l_datau_comparefn) =>l_result
Description
Sorts a list according to the specified comparison function. By default, the sort function uses alphalessp, which defaults to an alphabetical sort when u_comparefn is nil. This function does not create a new list. It returns the altered input list. This is a destructive operation. The l_data list is modified in place and no new storage is allocated. Pointers previously pointing to l_data may not be pointing at the head of the sorted list.
Sorts the list l_data according to the sort function u_comparefn. u_comparefn( g_x g_y ) returns non-nil if g_x can precede g_y in sorted order, nil if g_y must precede g_x. If u_comparefn is nil, alphabetical order is used. The algorithm currently implemented in sort is based on recursive merge sort.
Arguments
|
Comparison function to determine which of any two elements should come first. |
Value Returned
Examples
a = list( 3 -1 8 2 )
sort( a lambda((x y) lessp( x y))) => (-1 2 3 8)
The following examples demonstrate the use of second argument as nil. In this case, sort would expect the data to be sorted as either a list of symbol or strings.
y = '(c a d b)
(sort y nil)
=> (a b c d)
y
=> (c d) ;no longer points to head of list
y = '(c a d b)
y = (sort y nil)
=> (a b c d)
y
=> (a b c d) ;reassignment points y to sorted list.
The following example demonstrates how to use a non-trivial sort function.
cells=list("b" "a" "d" "c")
data=makeTable("Data" -1)
data["a"]=0
data["b"]=1
data["c"]=2
data["d"]=3
cells=sort(cells lambda((x y) lessp(data[x] data[y])))
=> ("a" "b" "c" "d")
Related Topics
Return to top