Sort an array of composite structures: Difference between revisions
Content deleted Content added
added Ursala |
|||
Line 716:
# sort by the first element of each pair
lsort -index 0 $people</lang>
=={{header|Ursala}}==
The built in sort operator, -<, can be parameterized by an
anonymous field specification and/or a relational predicate.
<lang Ursala>#import std
#cast %sWLW
examples =
(
-<&l <('z','a'),('x','c'),('y','b')>, # sorted by the left
-<&r <('z','a'),('x','c'),('y','b')>) # sorted by the right</lang>
output:
<pre>(
<('x','c'),('y','b'),('z','a')>,
<('z','a'),('y','b'),('x','c')>)</pre>
a more verbose example, showing a list of records of a user defined type sorted by a named field:
<lang Ursala>#import std
person :: name %s value %s
people =
<
person[name: 'Marlyn Monroe',value: 'priceless'],
person[name: 'Victor Hugo',value: 'millions'],
person[name: 'Johnny Carson',value: 'up there']>
#cast _person%L
example = (lleq+ ~name~~)-< people</lang>
output:
<pre>
<
person[name: 'Johnny Carson',value: 'up there'],
person[name: 'Marlyn Monroe',value: 'priceless'],
person[name: 'Victor Hugo',value: 'millions']></pre>
|