sort/compare
[1/3] from: pguimond:wanadoo at: 6-Nov-2002 21:58
Hi
I try to sort a series of blocks on the second element of the block which represents
the age of individuals.
rebol []
person: make block! [ name age ]
damien: make person [ "Damien" 20 ]
fabien: make person [ "Fabien" 1 ]
list-of-person: [ Damien Fabien ]
age-comp: func [ a b ] [ (second a) < (second b) ]
sort/compare list-of-person :age-comp
Script Error: second expected series argument of type: series pair event money date object
port time tuple any-function
struct event
** Where: halt-view
** Near: probe second a
The comparator function works though!
>> age-comp Damien Fabien
== false
How can I sort my series on the age ?
thanks for help!
Philippe
[2/3] from: sunandadh::aol::com at: 7-Nov-2002 15:12
Phillipe
> I try to sort a series of blocks on the second element of the block which
> represents the age of individuals.
It fails because the block contains the two words:
[Damien Fabien]
not the two block to which they refer:
[ [ "Damien" 20 ] ["Fabien" 1] ]
So you are one level in indirection out. Try this:
person: make block! [ name age ]
damien: make person [ "Damien" 20 ]
fabien: make person [ "Fabien" 1 ]
list-of-person: [ Damien Fabien ]
age-comp: func [ a b ] [ (second get :a) < (second get :b) ]
sort/compare list-of-person :age-comp
Sunanda.
[3/3] from: carl:cybercraft at: 8-Nov-2002 10:10
On 07-Nov-02, guimond wrote:
> Hi
> I try to sort a series of blocks on the second element of the block
<<quoted lines omitted: 15>>
> How can I sort my series on the age ?
> thanks for help!
Hi Philippe,
You were almost there. The...
list-of-person: [ Damien Fabien ]
will just be a block containing the words Damien & Fabien, not the
contents of the Damien and Fabien blocks, as can be seen at the REBOL
console...
>> list-of-person: [ Damien Fabien ]
== [Damien Fabien]
>> probe list-of-person
[Damien Fabien]
== [Damien Fabien]
Reducing the block will give you what (I assume) you want though...
>> person: make block! [ name age ]
== [name age]
>> damien: make person [ "Damien" 20 ]
== ["Damien" 20]
>> fabien: make person [ "Fabien" 1 ]
== ["Fabien" 1]
>> list-of-person: reduce [ Damien Fabien ]
== [["Damien" 20] ["Fabien" 1]]
Your sort function will then work...
>> age-comp: func [ a b ] [ (second a) < (second b) ]
>> sort/compare list-of-person :age-comp
== [["Fabien" 1] ["Damien" 20]]
Hope that helps.
--
Carl Read
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted