TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: HelpLispinSeattle on January 12, 2009, 01:31:26 PM

Title: "sort" of a request
Post 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?
Title: Re: "sort" of a request
Post by: uncoolperson on January 12, 2009, 01:46:40 PM
you could write something to reformat the date to year-month-day, then sort, the unreformat and return the list
Title: Re: "sort" of a request
Post by: MP on January 12, 2009, 01:47:38 PM
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.

Code: [Select]
(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.
Title: Re: "sort" of a request
Post by: dustinthiesse on January 12, 2009, 02:01:38 PM
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
Title: Re: "sort" of a request
Post by: kpblc on January 12, 2009, 02:07:04 PM
Code: [Select]
(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)
Title: Re: "sort" of a request
Post by: MP on January 12, 2009, 02:08:37 PM
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.
Title: Re: "sort" of a request
Post by: HelpLispinSeattle on January 12, 2009, 02:14:18 PM
Wow!...Thank you guys. And I though it would be
really a long long code.
Title: Re: "sort" of a request
Post by: MP on January 12, 2009, 02:15:55 PM
that's what she said
Title: Re: "sort" of a request
Post by: T.Willey on January 12, 2009, 02:26:15 PM
that's what she said
Zing!!

Welcome to theSwamp HelpLispinSeattle.
Title: Re: "sort" of a request
Post by: dustinthiesse on January 12, 2009, 02:37:05 PM
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.
Title: Re: "sort" of a request
Post by: HelpLispinSeattle on January 12, 2009, 03:31:44 PM
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.



Title: Re: "sort" of a request
Post by: MP on January 12, 2009, 04:00:00 PM
Just to be clear, what does each pair of numbers in your date string represent?
Title: Re: "sort" of a request
Post by: HelpLispinSeattle on January 12, 2009, 04:11:16 PM

example:
Today's date-> 01-12-09   Bla bla bla

01=month
12=day of month
09=year
Title: Re: "sort" of a request
Post by: MP on January 12, 2009, 04:18:30 PM
I had transposed the month and day. Try this:

Code: [Select]
(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:

Code: [Select]
(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

Title: Re: "sort" of a request
Post by: HelpLispinSeattle on January 12, 2009, 04:30:33 PM
Yeah!...I like both of them. Thank you very much "lambda king"....I bet yah
she did'nt say that yet.
Title: Re: "sort" of a request
Post by: MP on January 12, 2009, 04:36:08 PM
I bet yah she did'nt say that yet.

She did, but she said it to Tony first.
Title: Re: "sort" of a request
Post by: HelpLispinSeattle on January 12, 2009, 04:42:28 PM
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   ...
Title: Re: "sort" of a request
Post by: MP on January 12, 2009, 05:04:35 PM
If I understood your correctly ...

Code: [Select]
(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.
Title: Re: "sort" of a request
Post by: HelpLispinSeattle on January 12, 2009, 05:58:52 PM
Perfect!...Thank you MP...Thank you Tony!