[REBOL] Re: Simple sorting
From: greggirwin:mindspring at: 25-Sep-2002 16:02
Hi Sean,
<< How do I get the code, listing: sort read %., to sort alphabetically by
directories first then by filenames? So, the result looks like this:
[%dir1/ %dir2/ %dir3/ %file1 %file2 %file3] >>
I don't know that there's a single switch to do something like that. Maybe
someone else knows a nifty trick I don't. In any case, I whipped this up
real quick to show how you *could* do it. It uses a function I have to split
blocks into sub-blocks based on predicates.
dir-file-sort: func [
{Returns the block of files with directories first, followed by
files, with each group sorted.}
block [any-block!]
/local result
][
result: copy []
foreach blk split block [:dir?] none [
append result sort blk
]
]
split: func [
{Applies each predicate to each element in blk and returns
a block of blocks with items partitioned by which predicate
they match. If an item doesn't match any predicate, it will
be in the last block. Only handles simple args right now.}
series [series!]
predicates [block!]
args
/local match result p
][
result: copy []
loop add length? predicates 1 [append/only result copy []]
repeat el series [
match: false
repeat i length? predicates [
; Have to use a temp var for the predicate here, in addition
to
; the result of the predicate call.
p: get predicates/:i
match: p :el either block? args [args/:i][args]
if match [
append result/:i :el
break
]
]
if not match [
append last result :el
]
]
result
]
;split [1 2 3.4 5.6 7 8.9] [integer? decimal?] none
;split [1 2 3.4 5.6 7 8.9 0 100] [lesser? greater?] [3 7]
;split [1 2 3.4 5.6 7 8.9 0 100] [lesser? greater?] 3
I only tested this briefly but it should give you a starting point if
nothing else.
BIG CAVEAT: DIR? needs full path info if you're working on a list of files
that aren't in the current directory.
HTH!
--Gregg