Author Topic: Sorting list of list with Lambda function  (Read 2047 times)

0 Members and 1 Guest are viewing this topic.

Lost_in_()

  • Guest
Sorting list of list with Lambda function
« on: August 30, 2016, 12:06:49 AM »
((602 602000002 Unsorted 602 393361.75 6462327.52 1 6 1 0 3C93) (601 6010000000 Unsorted 393362.42 6462545.33 1 6 1 0 3C8C) (601 6010006007
Mercure Perth 392616.99 6463873.02 0.5 6 1 0 2D44) (602 6020049148 The Royal On The Waterfront  393962.19 6464248.67 1 6 1 0 2AD5) (602 6020007831 The
Dutch Trading Co 395487.69 6462162.6 1 6 1 0 2827) (602 6020137456 Rockpool Bar & Grill Perth 395512.04 6463352.84 1 6 1 0 2819) (603 6030130659 CBD IGA Plus
Liquor 392447.72 6463977.38 0.5 6 1 0 2587) (603 6030138819 Vintage Wine Sales 393439.89 6464831.22 1 6 1 0 2580) (603 6030002774 John's Food & Liquor Store 
393555.46 6464035.13 1 6 1 0 2564) (603 6030002915 BWS - Beer Wine Spirits East Perth  393358.98 6463605.11 0.5 6 1 0 255D) (603 6030004192 Vintage Cellars East Perth  393982.2 6464188.84 1 6 1 0 2556) ) (622 6220047894 Goodearth Hotel 393142.4 6463525.9 0.5 6 1 0 1B00) (622 6220029587 Crown Metropol 395493.24 6463374.86 1 6 1 0 1A90) (636 6360028324 Badlands Bar 392956.22 6464465 0.5 6 1 0 1A58) (637 6370029082 Gilkison Dance Studio 392626.21 6464016.45 0.5 6 1 0 1A3C))
Given the above list I am trying to write a sorted list of list routine and think I am nearly there, but I keep getting an error in the Lambda function - should be all 601's first , then 602's and so on.  Never used lambda before and getting a headache  :lol:  Any help appreciated

Code - Auto/Visual Lisp: [Select]
  1. (defun orderlist()  ;; takes p_str_list and orders list by p_lic_type
  2.   (princ "\nOrder list ")
  3.   (disp "p_str_list" p_str_list)
  4.   (setq p_sort nil)
  5.   (setq p_index (vl-sort-i p_str_list
  6.                  (lambda (p_a p_b)
  7.                    (< (nth 0 p_a) (nth 0 p_b))
  8.                    )
  9.                   )
  10.         )
  11.   (disp "p_str_list" p_str_list)
  12.   (disp "p_index " p_index)
  13.   (foreach p_n p_index (setq p_sort (cons (nth p_n p_str_list) p_sort)))
  14.   (reverse p_sort)
  15.   (disp "p_sort" p_sort)
  16.   )
  17.  

Lost_in_()

  • Guest
Re: Sorting list of list with Lambda function
« Reply #1 on: August 30, 2016, 12:24:41 AM »
Think it's sorted - missed having a function call to the lambda!
Code - Auto/Visual Lisp: [Select]
  1. (function (lambda (p_a p_b)
  2.  


kpblc

  • Bull Frog
  • Posts: 396
Re: Sorting list of list with Lambda function
« Reply #2 on: August 30, 2016, 01:39:21 AM »
Check this
Sorry for my English.

Lost_in_()

  • Guest
Re: Sorting list of list with Lambda function
« Reply #3 on: August 30, 2016, 08:56:45 AM »
Thanks Kpblc - much smaller than my effort

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Sorting list of list with Lambda function
« Reply #4 on: August 30, 2016, 09:19:31 AM »
Could also do this to do some more sorting :)
Code - Auto/Visual Lisp: [Select]
  1. (vl-sort lst
  2.          (function (lambda (a b)
  3.                      (< (apply 'strcat (mapcar 'vl-princ-to-string a))
  4.                         (apply 'strcat (mapcar 'vl-princ-to-string b))
  5.                      )
  6.                    )
  7.          )
  8. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lost_in_()

  • Guest
Re: Sorting list of list with Lambda function
« Reply #5 on: August 30, 2016, 08:55:08 PM »
According to my documentation vl-sort may remove duplicates (I am using Autocad 2008 - don't ask!) which would not be acceptable.

from the help file -
Return Values

A list containing the elements of list in the order specified by comparison-function. Duplicate elements may be eliminated from the list.


Regards
Pete

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sorting list of list with Lambda function
« Reply #6 on: August 30, 2016, 09:45:12 PM »
Could also do this to do some more sorting :)
Code - Auto/Visual Lisp: [Select]
  1. (vl-sort lst
  2.          (function (lambda (a b)
  3.                      (< (apply 'strcat (mapcar 'vl-princ-to-string a))
  4.                         (apply 'strcat (mapcar 'vl-princ-to-string b))
  5.                      )
  6.                    )
  7.          )
  8. )

Indeed. Specialized temporary sorting keys can be constructed that are specific to the data / app.

That said, better to apply it once, use it, then discard it, rather than suffer a performance hit each iteration thru the sort algorythm.

Simplified demo for ease of understanding for the op:

Code: [Select]
(mapcar 'cdr
    (vl-sort
        (mapcar
            (function (lambda (i) (cons (vl-princ-to-string i) i)))
            lst
        )           
        (function (lambda (a b) (< (car a) (car b))))
    )
)

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

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Sorting list of list with Lambda function
« Reply #7 on: August 30, 2016, 10:49:29 PM »
That's why you're the OG .  :-) thanks.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sorting list of list with Lambda function
« Reply #8 on: August 30, 2016, 11:06:55 PM »
lol -- thanks Ron (I think) -- I'm scared to ask -- what is OG -- Old/Original Geezer? Goof? Goober? Gaffe? Grump? Grok? :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Sorting list of list with Lambda function
« Reply #9 on: August 30, 2016, 11:25:22 PM »
Original Gangster 😎

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sorting list of list with Lambda function
« Reply #10 on: August 30, 2016, 11:30:23 PM »
Ha, too kind / cool.  8-)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Sorting list of list with Lambda function
« Reply #11 on: August 31, 2016, 01:41:00 PM »
FWIW, an alternative might be to use vl-sort-i, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda ( n ) (nth n lst)) (vl-sort-i (mapcar 'vl-princ-to-string lst) '<))

Lost_in_()

  • Guest
Re: Sorting list of list with Lambda function
« Reply #12 on: August 31, 2016, 08:42:39 PM »
Thanks for that Lee.  As I have that part of the code running now so will leave alone   :-)

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Sorting list of list with Lambda function
« Reply #13 on: September 01, 2016, 09:02:46 AM »
Could also do this to do some more sorting :)
Code - Auto/Visual Lisp: [Select]
  1. (vl-sort lst
  2.     (function (lambda (a b)
  3.            (<   (apply 'strcat (mapcar 'vl-princ-to-string a))
  4.          (apply 'strcat (mapcar 'vl-princ-to-string b))
  5.            )
  6.          )
  7.     )
  8. )

Indeed. Specialized temporary sorting keys can be constructed that are specific to the data / app.

That said, better to apply it once, use it, then discard it, rather than suffer a performance hit each iteration thru the sort algorythm.

Simplified demo for ease of understanding for the op:

Code: [Select]
(mapcar 'cdr
    (vl-sort
        (mapcar
            (function (lambda (i) (cons (vl-princ-to-string i) i)))
            lst
        )           
        (function (lambda (a b) (< (car a) (car b))))
    )
)

Cheers.
Just for grins, I benched the two functions with that short list ... quite a difference!  :o
Quote
    (MAPCAR (QUOTE CDR) (VL-SORT (MAPCAR...)......1422 / 16.70 <fastest>
    (VL-SORT LST (FUNCTION (LAMBDA (A B)...).....23750 / 1.00 <slowest>

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sorting list of list with Lambda function
« Reply #14 on: September 01, 2016, 05:48:06 PM »
Cool, who knew. :whistling: Thanks Ron :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst