tags:

views:

43

answers:

2

I want to sort this block on date column:

["domain1.com" 18-Jan-2011 #"^/" 
"domain2.com" 20-Aug-2011 #"^/" 
"domain3.com" 23-Dec-2011 #"^/" 
"domain4.com" 22-Sep-2011 #"^/"]

I can't see how to really do so with sort/skip function explained here, it's not crystal clear for me: http://www.rebol.com/docs/words/wsort.html

+1  A: 

You want the /all refinement used with a comparator function. That gets it to pass the subseries (which is as long as the skip length) to the comparator as the "record", instead of just passing the first element of that series.

>> sort/skip/compare/all ["domain1.com" 18-Jan-2011 #"^/" 
    "domain2.com" 20-Aug-2011 #"^/" 
    "domain3.com" 23-Dec-2011 #"^/" 
    "domain4.com" 22-Sep-2011 #"^/"] 3 func [a b] [
        (second a) < (second b)
    ]

== ["domain1.com" 18-Jan-2011 #"^/" 
    "domain2.com" 20-Aug-2011 #"^/" 
    "domain4.com" 22-Sep-2011 #"^/" 
    "domain3.com" 23-Dec-2011 #"^/]

It works in Rebol 2 but in the version of Rebol 3 I'm currently running, it's not working. That's a bug.

Hostile Fork
+3  A: 

You've got groups of three fields, and you want to sort on field 2?

This should do it:

data: [
    "domain1.com" 18-Jan-2011 #"^/" 
    "domain2.com" 20-Aug-2011 #"^/" 
    "domain3.com" 23-Dec-2011 #"^/" 
    "domain4.com" 22-Sep-2011 #"^/"
   ]

 sort/skip/compare data 3 2
Sunanda
Ah, never absorbed you could use just an offset there. Still even in latest r3 *sort/skip/compare/all ["a" 2 "b" 1] 2 func [a b] [print a print b]* seems to be doing the wrong thing (prints a on one line and b on one line)
Hostile Fork
Thanks that seems obvious afterwards the help should be as clear as you :)
Rebol Tutorial
@hostile glad this made you discover a potential bug in R3; as for me I'm still using R2.
Rebol Tutorial
You can use more than one offset too.... sort/skip/compare data 3 [2 1] .... would sort by date then by domain. The /compare func method is useful when you need even more fine-tuning on how you chose the sort ordering,
Sunanda
That's even greater.
Rebol Tutorial