sort a numeric list

  • Thread starter Thread starter liftedaxis
  • Start date Start date
L

liftedaxis

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
 
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
 
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.
 
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


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 Piercey said:
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" <tony.tanzillo at caddzone dot com>
Newsgroups: autodesk.autocad.customization
References: <[email protected]>
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 <list-of-points> '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.
 
Use vl-sort
More info in the Autolisp reference.

JanC

liftedaxis said:
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?
 
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
 
Back
Top