sort a numeric list

Discussion in 'AutoCAD' started by liftedaxis, Feb 20, 2004.

  1. liftedaxis

    liftedaxis Guest

    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
     
    liftedaxis, Feb 20, 2004
    #1
  2. liftedaxis

    liftedaxis Guest

    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
     
    liftedaxis, Feb 20, 2004
    #2
  3. 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, Feb 20, 2004
    #3
  4. 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.
     
    R. Robert Bell, Feb 20, 2004
    #4
  5. Here's one:

    ---------------------------------------------------------------------
    From: "Tony Tanzillo" <tony.tanzillo at caddzone dot com>
    Newsgroups: autodesk.autocad.customization
    References: <>
    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.
     
    Tony Tanzillo, Feb 20, 2004
    #5
  6. liftedaxis

    Jan C Guest

    Use vl-sort
    More info in the Autolisp reference.

    JanC

    to do this. there's acad_strlsort on the string side, but what if i want to
    sort a numeric list?
     
    Jan C, Feb 21, 2004
    #6
  7. 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.



    AcadX for AutoCAD 2004 Beta 1
    http://mysite.verizon.net/~vze2vjds/acadx/AcadX16.zip
     
    Tony Tanzillo, Feb 22, 2004
    #7
  8. liftedaxis

    liftedaxis Guest

    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
     
    liftedaxis, Feb 23, 2004
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.