TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: HelpLispinSeattle on January 12, 2009, 01:31:26 PM
-
Hi,
I've been searching every where for a lisp "sort" function . We archive are drawings
in this folder format.
12-08-07 75% Design Development
08-08-07 Design Development
03-08-08 Progress Print
07-08-08 For Construction
12-08-08 For Permit & Pricing
01-08-09 Addendum No.1
But if I use "acad_strlsort" (I've also tried vl-sort, win_sort & dos_sortlist)
it returns....
01-08-09 Addendum No.1
03-08-08 Progress Print
07-08-08 For Construction
08-08-07 Design Development
12-08-07 75% Design Development
12-08-08 For Permit & Pricing
I would like it to sort by year & chronologically....
08-08-07 Design Development
12-08-07 75% Design Development
03-08-08 Progress Print
07-08-08 For Construction
12-08-08 For Permit & Pricing
01-08-09 Addendum No.1
Can somebody help?
-
you could write something to reformat the date to year-month-day, then sort, the unreformat and return the list
-
There's 2 approaches (1) normalize the data so a normal sort works properly (2) create a custom sort.
While it's fun to do the latter, it means every time you have wonky data you have to write a new sort function, and as easy as it is to do the latter, I'd recommend the former.
Having said that, here's a sort routine do do what you want to dispel the notion I'm just being a curmudgeon.
(setq lst
'(
"12-08-07 75% Design Development"
"08-08-07 Design Development"
"03-08-08 Progress Print"
"07-08-08 For Construction"
"12-08-08 For Permit & Pricing"
"01-08-09 Addendum No.1"
)
)
(defun foo ( lst )
( (lambda (f) (vl-sort lst '(lambda (a b) (< (f a) (f b)))))
(lambda (x) (strcat (substr x 7 2) (substr x 1 2)(substr x 4 2)))
)
)
(mapcar 'print (foo lst))
"08-08-07 Design Development"
"12-08-07 75% Design Development"
"03-08-08 Progress Print"
"07-08-08 For Construction"
"12-08-08 For Permit & Pricing"
"01-08-09 Addendum No.1"
I could have converted the date strings to numerical values and sorted on same but in the interests of brevity I didn't.
Edit: Revised per this (http://www.theswamp.org/index.php?topic=26793.msg322819#msg322819) post.
-
I really need to learn the lambda function cuz that right there just blows me away.
I can't even process what that function is doing. :-o
-
(setq value '("12-08-07 75% Design Development" "08-08-07 Design Development"
"03-08-08 Progress Print" "07-08-08 For Construction"
"12-08-08 For Permit & Pricing" "01-08-09 Addendum No.1"
)
) ;_ end of setq
(defun test (lst)
(vl-sort
lst
(function
(lambda (a b)
(< (strcat (substr a 7 2) (substr a 4 2) (substr a 1 2))
(strcat (substr b 7 2) (substr b 4 2) (substr b 1 2))
) ;_ end of <
) ;_ end of lambda
) ;_ end of function
) ;_ end of vl-sort
) ;_ end of defun
;; (test value)
-
I really need to learn the lambda function cuz that right there just blows me away.
I can't even process what that function is doing. :-o
It's nothing magical, maybe this (http://www.theswamp.org/index.php?topic=2953.msg37000#msg37000) will help.
-
Wow!...Thank you guys. And I though it would be
really a long long code.
-
that's what she said
-
that's what she said
Zing!!
Welcome to theSwamp HelpLispinSeattle.
-
I really need to learn the lambda function cuz that right there just blows me away.
I can't even process what that function is doing. :-o
It's nothing magical, maybe this (http://www.theswamp.org/index.php?topic=2953.msg37000#msg37000) will help.
I get the smaller uses of it, but I just can't seem to wrap my head around this one. Maybe I just need try using it more in my own coding and I will get more comfortable with it. Right now it's like a scary monster to me.
-
MP,
I have a project that has around 150 archived folders. "Foo" got the days & year
correctly but not the months (the first two characters). They did not sort correctly.
-
Just to be clear, what does each pair of numbers in your date string represent?
-
example:
Today's date-> 01-12-09 Bla bla bla
01=month
12=day of month
09=year
-
I had transposed the month and day. Try this:
(defun foo ( lst )
( (lambda (f) (vl-sort lst '(lambda (a b) (< (f a) (f b)))))
(lambda (x) (strcat (substr x 7 2) (substr x 1 2)(substr x 4 2)))
)
)
And for <more> obfuscated fun, this:
(defun foo ( lst )
( (lambda (f1 f2) (vl-sort lst 'f1))
(lambda (a b) (< (f2 a) (f2 b)))
(lambda (x) (strcat (substr x 7 2) (substr x 1 2)(substr x 4 2)))
)
)
:D
-
Yeah!...I like both of them. Thank you very much "lambda king"....I bet yah
she did'nt say that yet.
-
I bet yah she did'nt say that yet.
She did, but she said it to Tony first.
-
MP,
I dont know who Tony is but I'm smiling here...One last adjustment.
I reverse the list but it did not come out the way I liked it. Could you
please make work like this. (The newest date on top.)
01-12-09 Bla bla bla
12-22-08 Bla bla bla
12-10-08 Bla bla bla
06-10-08 Bla bla bla
06-09-08 Bla bla bla
11-17-07 ...
10-10-07 ...
-
If I understood your correctly ...
(defun foo ( lst )
( (lambda (f) (vl-sort lst '(lambda (a b) (< (f b) (f a)))))
(lambda (x) (strcat (substr x 7 2) (substr x 1 2)(substr x 4 2)))
)
)
Tony is a reference to Tony Tanzillo, who has contributed to the body of knowledge the AutoCAD programming community enjoys to such a degree that any attempt to acknowledge same would fall pathetically short. Regardless, you and I are in his debt.
-
Perfect!...Thank you MP...Thank you Tony!