I don't know if it is correct to use local variables in the sort function
(why not ?)
anyway they shouldn't be necessary
look at this example
rebol [
title: "sort test"
]
; after running this script, invoke sort1, sort2 or sort3
; if you have grouped records, that is data is organized as a serie of
series
; you can call directly sort/compare
; auxiliary function to see what happens
; remove where is called in a real application
dump: func [X Y] [ print [ ">" x "-" y "<" ] ]
data1: [
["John" "Kennedy"]
["Greta" "Garbo" ]
["Julie" "Andrews"]
["Dominique" "Sanda" ]
]
fsort: func [X Y] [ dump X Y (second X) < (second Y) ]
sort1: func [] [print sort/compare data1 :fsort]
; if you have a simple record, that is data is visually separated, but
inside a single serie,
; and you want to sort on a field different from the first, you can
; first groups data in a serie of series, and then sort them.
; This is precisely what the record-sort function does:
; it creates a serie of series , sorts it, and then decompacts it
data2: [
"John" "Kennedy"
"Greta" "Garbo"
"Julie" "Andrews"
"Dominique" "Sanda"
]
record-sort: func [record [block!] num [integer!]] [
tmp: copy []
new: copy []
foreach [a b ] record [append/only tmp reduce [a b]]
sort-method: func [a b] [ dump a b (at a num) < (at b num) ]
foreach rec sort/compare tmp :sort-method [append new rec]
return new
]
sort2: func [] [ print record-sort data2 1 ]
sort3: func [] [ print record-sort data2 2 ]
-Messaggio originale-
Da: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
A: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Data: lunedì 1 maggio 2000 21.10
Oggetto: [REBOL] Of objects, sorts, compares and locals
>>- Open Your Mind -<
>
>
>
>I've been trying to make a multiple-key sorting routine for a block of
objects and I had a couple of problems. Now the routine works, but I'm not
completely satisfied with my solution and I'm still curious about the
problems.
>
>
>1.
>As a first step, just to see the result, I began by sorting n times, least
significant key first. This is a slow process, but it usually works. In this
case, though, it didn't. It resulted in the block sorted by the most
significant key only, ignoring the other keys.
>I guess this has something to do with the inner workings of the sort
function, but...
>
>
>2.
>OK, bypassing completely the quick and dirty solution, I wrote a
sort-method function that does it all in one pass only, but it doesn't like
local variables. I use a variable to store an intermediate result, but if I
specify that variable as local, REBOL aborts after some iterations, yielding
this:
>
>> Invalid data type during recycle
>> ** Press enter to quit.
>
>The series to sort is a block of 59 objects (for now), sorted with
something like this (which also aborts):
>
>sort-method: func [a b /local c] [c: a/type < b/type c]
>sort/compare database :sort-method
>
>Is this a bug or just me missing something totally obvious? Garbage
collection? Garbage in my mind?
>
>
>For now, I've eliminated the /local refinement from the sort-method. :-(
>
>
>
>
>Alessandro Pini ([EMAIL PROTECTED])
>
>"I think I will stick my head in the station's fusion reactor. It would be
quicker!" (Mollari)
>
>