[REBOL] Sorting an object based on one field.
From: sunandadh:aol at: 12-Jan-2002 19:20
Hi louis
> How do you sort objects based on one field within the object. I keep
> running into situations where this would really be helpful. For instance,
> below I want to sort a directory based on date. The following code
doesn't
> work, but I give it so you pros can see what I'm trying to do.
I had a similar problem some time ago. I don't know if there's a better way,
but i wrote a function that sorted the block of objects. My function is
below. It assumes each object has a word called tc-Sortid. Amend as
appropriate.
Sort-List: func [list-to-sort [Block!]
/local block-index sort-keys sorted-list]
;; ------------------------------------------
;; Ascending sort of a block of objects
;; according to their TC-sort-id field
;; ------------------------------------------
[
if 0 = length? list-to-sort
[Return copy [] ]
block-index: 0
Sort-keys: copy []
foreach fx list-to-sort
[
block-index: block-index + 1
repend/only Sort-keys [fx/TC-sortid block-index]
]
sort Sort-keys
sorted-list: copy []
foreach fx Sort-keys
[
append sorted-list pick list-to-sort fx/2
]
Return sorted-list
] ; func
Sunanda