Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

sort a numeric list

7 REPLIES 7
Reply
Message 1 of 8
liftedaxis
154 Views, 7 Replies

sort a numeric list

it must be late in the day, but then again, i don't remember a quick way to do this. there's acad_strlsort on the string side, but what if i want to sort a numeric list?
--Jeremiah
7 REPLIES 7
Message 2 of 8
liftedaxis
in reply to: liftedaxis

okay, yes, that was a brain fade.
i'm sorry, the example for vl-sort is perfect.
sorry for wasting your time in reading this. 🙂

--Jeremiah
Message 3 of 8
Anonymous
in reply to: liftedaxis

Becareful with vl-sort. There have been many discussions here in this group on how dangerous this function can be. Quick example: (vl-sort '(1 3 2 1) '<) (1 2 3) Notice on the removal of duplicates. -- -Jason Member of the Autodesk Discussion Forum Moderator Program "liftedaxis" wrote in message news:20139699.1077237141303.JavaMail.jive@jiveforum1.autodesk.com... > okay, yes, that was a brain fade. > i'm sorry, the example for vl-sort is perfect. > sorry for wasting your time in reading this. :) > > --Jeremiah
Message 4 of 8
Anonymous
in reply to: liftedaxis

Here is a replacement that doesn't have that problem: ;| ------------------------------------------ Alternative to a plain (vl-sort) This function does _not_ remove duplicates Written by R. Robert Bell ------------------------------------------ |; (defun rrbI:Sort (lst func) (mapcar '(lambda (ndx) (nth ndx lst)) (vl-Sort-I lst func))) -- R. Robert Bell, MCSE www.AcadX.com "Jason Piercey" wrote in message news:403614ce_2@newsprd01... Becareful with vl-sort. There have been many discussions here in this group on how dangerous this function can be. Quick example: (vl-sort '(1 3 2 1) '<) (1 2 3) Notice on the removal of duplicates. -- -Jason Member of the Autodesk Discussion Forum Moderator Program "liftedaxis" wrote in message news:20139699.1077237141303.JavaMail.jive@jiveforum1.autodesk.com... > okay, yes, that was a brain fade. > i'm sorry, the example for vl-sort is perfect. > sorry for wasting your time in reading this. :) > > --Jeremiah
Message 5 of 8
Anonymous
in reply to: liftedaxis

"Jason Piercey" wrote in message news:403614ce_2@newsprd01... > Becareful with vl-sort. There have been many > discussions here in this group on how dangerous > this function can be. Here's one: --------------------------------------------------------------------- From: "Tony Tanzillo" Newsgroups: autodesk.autocad.customization References: <3E78800E.C7972757@bbsae.com> Subject: Re: How can I sort a list of 3d points? Date: Wed, 19 Mar 2003 10:37:20 -0500 Lines: 48 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 You can use vl-sort, with a comparison function that weights the ordinates in whatever way you want. Here's an example that gives the greatest weight to the X ordinate, and the least weight to the Z ordinate: (setq fuzz 1.0e-6) ;; comparison precision ;; sort on three keys (x, y, and z) (defun compare-points (a b) (if (equal (car a) (car b) fuzz) (if (equal (cadr a) (cadr b) fuzz) (> (caddr a) (caddr b)) (> (cadr a) (cadr b)) ) (> (car a) (car b)) ) ) (vl-sort 'compare-points) If you search this newsgroup, you'll find a much more powerful sorting function along with a good discussion on why (vl-sort) can be very dangerous. For that reason, I suggest you replace the built-in vl-sort with this: (defun vl-sort (lst func) (mapcar '(lambda (x) (nth x lst)) (vl-sort-i lst func) ) ) This will ensure that (vl-sort) does not remove elements that it sees as equal. "Mike Hutchinson" wrote in message news:3E78800E.C7972757@bbsae.com... > some pointers to get me started please > how can I sort a list of points? > --------------------------------------------------------------- -- AcadXTabs: MDI Document Tabs for AutoCAD http://www.acadxtabs.com AcadX for AutoCAD 2004 Beta 1 http://mysite.verizon.net/~vze2vjds/acadx/AcadX16.zip > > Quick example: > > (vl-sort '(1 3 2 1) '<) > > (1 2 3) > > Notice on the removal of duplicates. > > -- > > -Jason > Member of the Autodesk Discussion Forum Moderator Program > > > "liftedaxis" wrote in message > news:20139699.1077237141303.JavaMail.jive@jiveforum1.autodesk.com... > > okay, yes, that was a brain fade. > > i'm sorry, the example for vl-sort is perfect. > > sorry for wasting your time in reading this. :) > > > > --Jeremiah > >
Message 6 of 8
Anonymous
in reply to: liftedaxis

Use vl-sort More info in the Autolisp reference. JanC "liftedaxis" schreef in bericht news:365988.1077237044098.JavaMail.jive@jiveforum1.autodesk.com... > it must be late in the day, but then again, i don't remember a quick way to do this. there's acad_strlsort on the string side, but what if i want to sort a numeric list? > --Jeremiah
Message 7 of 8
Anonymous
in reply to: liftedaxis

"Jan C" wrote in message news:4037e7f2_2@newsprd01... > Use vl-sort > More info in the Autolisp reference. Sometimes it helps to review the responses to questions before responding. For sorting integers, the VL-SORT function should *never* be used. See the other posts in this thread for more info. -- AcadXTabs: MDI Document Tabs for AutoCAD http://www.acadxtabs.com AcadX for AutoCAD 2004 Beta 1 http://mysite.verizon.net/~vze2vjds/acadx/AcadX16.zip
Message 8 of 8
liftedaxis
in reply to: liftedaxis

now i'm confused. "never" is a pretty brash statement.
i'm sorting unique integers, and as such, vl-sort works fine. so rather than "never", how about, "only if removing duplicates is not an issue."
or is there some other reason regarding integer sorting that is critically important?

--Jeremiah

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost