Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Sorting an object based on one field.

 [1/6] from: louisaturk::eudoramail::com at: 12-Jan-2002 17:00


Hi rebols, 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. dir: [] foreach file read %. [ info: info? file append dir make object! [date: info/date file_name: file] ] sort dir foreach di dir [ if not find di/file_name {/} [ print di/date ] ] Thanks, Louis

 [2/6] 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

 [3/6] from: brett:codeconscious at: 13-Jan-2002 11:43


Hi, SORT has a compare refinement. So use sort on the series but supply a function that will compare your objects. See "Comparison Functions" in "Sorting Series" of the Rebol/Core documentation. So using your example: dir: [] foreach file read %. [ info: info? file append dir make object! [date: info/date file_name: file] ] sort/compare dir func[a b][lesser? a/date b/date] We can make a function that sorts series given a field name: sort-object-series: func [ "Sorts a series of objects" series [series!] field [word!] ][ sort/compare series func[a b][lesser? get in a field get in b field] ] Now we can do this: sort-object-series dir 'date sort-object-series dir 'file_name Cheers Brett.

 [4/6] from: tomc:darkwing:uoregon at: 12-Jan-2002 18:48


/compare is your friend just somrthing like sort/compare object-list func[a b][a/path-to-field <= b/path-to-field] should be fine On Sat, 12 Jan 2002 [SunandaDH--aol--com] wrote:

 [5/6] from: sunandadh:aol at: 13-Jan-2002 3:37


Hi Tom,
> /compare is your friend > > just somrthing like > > sort/compare object-list func[a b][a/path-to-field <= b/path-to-field] > > should be fine
Thank you. I should have done what Louis did and ask the list before rolling my own. Reinventing the wheel isn't always a bad thing, but in this case my wheel was squarer than the available product. Which is a good cue for me to step on my soap (not SOAP) box and mention something that's been annoying me for months. Handy hints and tips and "how tos?" like this pop up regularly on this list (that's one of it's main strengths?) and then disappear into the archive forever. Of course they could be found by someone doing a determined and exhaustive search. But how many postings containing "sort" would you have to read to find this tip? How is anyone starting Rebol next week ever going to stumble across Volver's brilliant vv? It's not a new suggestion (I know, cos I've made it twice before, though not on this list) that somewhere we have a "Handy hints" page that preserves and showcases all the small simple "tricks" that give Rebol such depth. Sunanda.

 [6/6] from: louisaturk:eudoramail at: 16-Jan-2002 14:32


Brett, Tom, and Sunanda, Many thanks for helping me past this problem. You guys always make the solutions so simple. Sorry to take to long to respond, but other duties are taking most of my time this week. Louis At 11:43 AM 1/13/2002 +1100, you wrote: