Author Topic: "sort" of a request  (Read 6724 times)

0 Members and 1 Guest are viewing this topic.

HelpLispinSeattle

  • Guest
"sort" of a request
« 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?

uncoolperson

  • Guest
Re: "sort" of a request
« Reply #1 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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "sort" of a request
« Reply #2 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 post.
« Last Edit: January 12, 2009, 04:23:52 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

dustinthiesse

  • Guest
Re: "sort" of a request
« Reply #3 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

kpblc

  • Bull Frog
  • Posts: 396
Re: "sort" of a request
« Reply #4 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)
Sorry for my English.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "sort" of a request
« Reply #5 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 will help.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

HelpLispinSeattle

  • Guest
Re: "sort" of a request
« Reply #6 on: January 12, 2009, 02:14:18 PM »
Wow!...Thank you guys. And I though it would be
really a long long code.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "sort" of a request
« Reply #7 on: January 12, 2009, 02:15:55 PM »
that's what she said
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: "sort" of a request
« Reply #8 on: January 12, 2009, 02:26:15 PM »
that's what she said
Zing!!

Welcome to theSwamp HelpLispinSeattle.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

dustinthiesse

  • Guest
Re: "sort" of a request
« Reply #9 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 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.

HelpLispinSeattle

  • Guest
Re: "sort" of a request
« Reply #10 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.




MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "sort" of a request
« Reply #11 on: January 12, 2009, 04:00:00 PM »
Just to be clear, what does each pair of numbers in your date string represent?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

HelpLispinSeattle

  • Guest
Re: "sort" of a request
« Reply #12 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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "sort" of a request
« Reply #13 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

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

HelpLispinSeattle

  • Guest
Re: "sort" of a request
« Reply #14 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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "sort" of a request
« Reply #15 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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

HelpLispinSeattle

  • Guest
Re: "sort" of a request
« Reply #16 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   ...

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "sort" of a request
« Reply #17 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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

HelpLispinSeattle

  • Guest
Re: "sort" of a request
« Reply #18 on: January 12, 2009, 05:58:52 PM »
Perfect!...Thank you MP...Thank you Tony!