<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: vl-sort a list of lists in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13423675#M1092</link>
    <description>&lt;P&gt;I would take a look, but so I don't have to start from scratch, post a link to the Cadtutor place with the code, or the code itself if it's not too long.&amp;nbsp; [I don't participate there, but I'm happy to see things spread around.&amp;nbsp; Whoever posted it must have gotten it from here somewhere -- a link to that somewhere would also suffice, if that's how they posted it.]&lt;/P&gt;</description>
    <pubDate>Sun, 13 Apr 2025 19:44:08 GMT</pubDate>
    <dc:creator>Kent1Cooper</dc:creator>
    <dc:date>2025-04-13T19:44:08Z</dc:date>
    <item>
      <title>vl-sort a list of lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13423649#M1090</link>
      <description>&lt;P&gt;I have a list of lists I would like to sort by two entries. I once had a need to do a sort and the forum helped a lot. So, I thought I would ask again. I thought I would first attempt the code I was once given and as I was studying how it worked and how I could use it, I found that vl-sort will remove duplicate items. And then someone posted code in CADTutor that Kent1Cooper had wrote using mapcar to retain the duplicates. His code is slightly above my abilities.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My list is:&lt;/P&gt;&lt;P&gt;(setq LST&lt;BR /&gt;'(((1 . "T1") (2 . "l-cre-non.dwg") (3 . 4) (4 . "CREPE MYRTLE") (5 . 8) (6 . 1))&lt;BR /&gt;((1 . "T2") (2 . "l-mag-non.dwg") (3 . 12) (4 . "MAGNOLIA") (5 . 24) (6 . 1))&lt;BR /&gt;((1 . "T3") (2 . "l-oak-non.dwg") (3 . 10) (4 . "OAK") (5 . 20) (6 . 1))&lt;BR /&gt;((1 . "T4") (2 . "l-app-non.dwg") (3 . 12) (4 . "APPLE") (5 . 24) (6 . 1))&lt;BR /&gt;((1 . "T5") (2 . "l-app-non.dwg") (3 . 8) (4 . "APPLE") (5 . 16) (6 . 2))&lt;BR /&gt;))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to first sort by association 4 (cdr (assoc 4 LST)), then by association 3 (cdr (assoc 3 LST))&lt;/P&gt;&lt;P&gt;The list should be returned as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(setq LST&lt;BR /&gt;'(((1 . "T5") (2 . "l-app-non.dwg") (3 . 8) (4 . "APPLE") (5 . 16) (6 . 2))&lt;BR /&gt;((1 . "T4") (2 . "l-app-non.dwg") (3 . 12) (4 . "APPLE") (5 . 24) (6 . 1))&lt;BR /&gt;((1 . "T1") (2 . "l-cre-non.dwg") (3 . 4) (4 . "CREPE MYRTLE") (5 . 8) (6 . 1))&lt;BR /&gt;((1 . "T2") (2 . "l-mag-non.dwg") (3 . 12) (4 . "MAGNOLIA") (5 . 24) (6 . 1))&lt;BR /&gt;((1 . "T3") (2 . "l-oak-non.dwg") (3 . 10) (4 . "OAK") (5 . 20) (6 . 1))&lt;BR /&gt;))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate any help given. Thanks.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Apr 2025 19:07:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13423649#M1090</guid>
      <dc:creator>dvertz</dc:creator>
      <dc:date>2025-04-13T19:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: vl-sort a list of lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13423668#M1091</link>
      <description>&lt;P&gt;Try these. It shouldn't remove any items.&lt;/P&gt;
&lt;P&gt;(setq lst (vl-sort lst '(lambda (e1 e2) (&amp;lt; (cdr (nth 2 e1)) (cdr (nth 2 e2))))))&lt;/P&gt;
&lt;P&gt;(setq lst (vl-sort lst '(lambda (e1 e2) (&amp;lt; (cdr (nth 3 e1)) (cdr (nth 3 e2))))))&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or you can use your assoc as well&lt;/P&gt;
&lt;P&gt;(setq lst (vl-sort lst '(lambda (e1 e2) (&amp;lt; (cdr (assoc 3 e1)) (cdr (assoc 3 e2))))))&lt;/P&gt;
&lt;P&gt;(setq lst (vl-sort lst '(lambda (e1 e2) (&amp;lt; (cdr (assoc 4 e1)) (cdr (assoc 4 e2))))))&lt;/P&gt;</description>
      <pubDate>Sun, 13 Apr 2025 19:41:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13423668#M1091</guid>
      <dc:creator>ВeekeeCZ</dc:creator>
      <dc:date>2025-04-13T19:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: vl-sort a list of lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13423675#M1092</link>
      <description>&lt;P&gt;I would take a look, but so I don't have to start from scratch, post a link to the Cadtutor place with the code, or the code itself if it's not too long.&amp;nbsp; [I don't participate there, but I'm happy to see things spread around.&amp;nbsp; Whoever posted it must have gotten it from here somewhere -- a link to that somewhere would also suffice, if that's how they posted it.]&lt;/P&gt;</description>
      <pubDate>Sun, 13 Apr 2025 19:44:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13423675#M1092</guid>
      <dc:creator>Kent1Cooper</dc:creator>
      <dc:date>2025-04-13T19:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: vl-sort a list of lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13424658#M1093</link>
      <description>&lt;P&gt;BeekeeCZ,&lt;/P&gt;&lt;P&gt;Thank you so much, I did not think it would be that simple. That is code I can actually understand (mostly). I thought that code would remove the duplicate "Apple".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kent1Cooper,&lt;/P&gt;&lt;P&gt;Thanks for the reply. BeekeeCZ appears to have the solution. I have only ran one test, so I'll need to run more to be sure. But currently it seems where I had worked out a bug in my code, another has presented itself in building the list of lists. More debugging to do. But if you would like to look at where you were mention (yes. they said it was from the Autodesk forums), here is the link: &lt;A title="https://www.cadtutor.net/forum/topic/78029-vl-sort-kinda-sucks/" href="https://www.cadtutor.net/forum/topic/78029-vl-sort-kinda-sucks/" target="_blank" rel="noopener"&gt;https://www.cadtutor.net/forum/topic/78029-vl-sort-kinda-sucks/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Apr 2025 12:05:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13424658#M1093</guid>
      <dc:creator>dvertz</dc:creator>
      <dc:date>2025-04-14T12:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: vl-sort a list of lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13424868#M1094</link>
      <description>&lt;P&gt;vl-sort removes duplicates only in lists of integers.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For your sample, you can use this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="lisp"&gt;(vl-sort lst
 '(lambda (a b)
    (if
      (eq (cdr (assoc 4 a)) (cdr (assoc 4 b)))
      (&amp;lt; (cdr (assoc 3 a)) (cdr (assoc 3 b)))
      (&amp;lt; (cdr (assoc 4 a)) (cdr (assoc 4 b)))
    )
  )
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Apr 2025 13:21:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13424868#M1094</guid>
      <dc:creator>MunteanStefan</dc:creator>
      <dc:date>2025-04-14T13:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: vl-sort a list of lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13424889#M1095</link>
      <description>&lt;P&gt;&lt;SPAN class=""&gt;&lt;A href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5558752" target="_self"&gt;&lt;SPAN class=""&gt;MunteanStefan&lt;/SPAN&gt;&lt;/A&gt;&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for that info. I will keep that in mind next time I attempt using vl-sort.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Apr 2025 13:31:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13424889#M1095</guid>
      <dc:creator>dvertz</dc:creator>
      <dc:date>2025-04-14T13:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: vl-sort a list of lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13426026#M1096</link>
      <description>&lt;P&gt;Just a comment do you really need the list to be a dotted pair ? Why not just a plain list, each item can still be found using nth x lst) and sort on (nth 3 lst)&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(1 . "T5") (2 . "l-app-non.dwg") (3 . 8) (4 . "APPLE") (5 . 16) (6 . 2))


("T5" "l-app-non.dwg" 8 "APPLE" 16 2)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(setq LST
'(
("T1" "l-cre-non.dwg" 4 "CREPE MYRTLE" 8 1)
("T2" "l-mag-non.dwg" 12 "MAGNOLIA" 24 1)
("T3" "l-oak-non.dwg" 10 "OAK" 20 1)
("T4" "l-app-non.dwg" 12 "APPLE" 24 1)
("T5" "l-app-non.dwg" 8  "APPLE" 16 2)
)
)

; sorts on two items
(setq lst (vl-sort lst
	 '(lambda (a b)
	    (cond
	      ((&amp;lt; (nth 3 a) (nth 3 b)))
	      ((= (nth 3 a) (nth 3 b)) 
		  (&amp;lt; (nth 4 a) (nth 4 b)))
	    )
	  )
))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Apr 2025 00:22:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vl-sort-a-list-of-lists/m-p/13426026#M1096</guid>
      <dc:creator>Sea-Haven</dc:creator>
      <dc:date>2025-04-15T00:22:41Z</dc:date>
    </item>
  </channel>
</rss>

