sort a list of points?..

Discussion in 'AutoCAD' started by C Witt, Aug 13, 2004.

  1. C Witt

    C Witt Guest

    ok.. i have a list of points that I need to sort twice.. once by x
    cord, and once by y cord.

    ((12.99999 6.81111 0.0)(4582.61795 3247.13846 0.0)(ect....))

    you get the idea..

    x cord will need to be smallest to largest, and y cord will be largest
    to smallest.

    now how?
     
    C Witt, Aug 13, 2004
    #1
  2. from vlisp help for vl-sort:

    Sort a list of 2D points by Y coordinate:

    (vl-sort '((1 3) (2 2) (3 1))
    (function (lambda (e1 e2)
    (< (cadr e1) (cadr e2)) ) ) )

    Gives
    ((3 1) (2 2) (1 3))


    C Witt <>
    |>ok.. i have a list of points that I need to sort twice.. once by x
    |>cord, and once by y cord.
    |>
    |>((12.99999 6.81111 0.0)(4582.61795 3247.13846 0.0)(ect....))
    |>
    |>you get the idea..
    |>
    |>x cord will need to be smallest to largest, and y cord will be largest
    |>to smallest.
    |>
    |>now how?

    James Maeding
    Civil Engineer/Programmer
     
    James Maeding, Aug 13, 2004
    #2
  3. C Witt

    C Witt Guest

    ok.. but one problem..

    (vl-sort '((27.7521 19.0702 0.0)
    (27.752 19.0702 0.0)
    (27.7519 19.0702 0.0)
    (27.7518 19.0702 0.0)
    (27.7521 18.0702 0.0))
    (function (lambda (e1 e2)
    (> (cadr e1) (cadr e2)))))

    returns a list of:
    ((27.7521 19.0702 0.0)
    (27.752 19.0702 0.0)
    (27.7519 19.0702 0.0)
    (27.7518 19.0702 0.0)
    (27.7521 18.0702 0.0))

    when I want a list of:
    ((27.7518 19.0702 0.0)
    (27.7519 19.0702 0.0)
    (27.752 19.0702 0.0)
    (27.7521 19.0702 0.0)
    (27.7521 18.0702 0.0))
     
    C Witt, Aug 13, 2004
    #3
  4. just change the cadr to whatever element you care about,
    car for x, cadr for y, caddr for z....

    C Witt <>
    |>ok.. but one problem..
    |>
    |>(vl-sort '((27.7521 19.0702 0.0)
    |>(27.752 19.0702 0.0)
    |>(27.7519 19.0702 0.0)
    |>(27.7518 19.0702 0.0)
    |>(27.7521 18.0702 0.0))
    |> (function (lambda (e1 e2)
    |> (> (cadr e1) (cadr e2)))))
    |>
    |>returns a list of:
    |>((27.7521 19.0702 0.0)
    |>(27.752 19.0702 0.0)
    |>(27.7519 19.0702 0.0)
    |>(27.7518 19.0702 0.0)
    |>(27.7521 18.0702 0.0))
    |>
    |>when I want a list of:
    |>((27.7518 19.0702 0.0)
    |>(27.7519 19.0702 0.0)
    |>(27.752 19.0702 0.0)
    |>(27.7521 19.0702 0.0)
    |>(27.7521 18.0702 0.0))

    James Maeding
    Civil Engineer/Programmer
     
    James Maeding, Aug 13, 2004
    #4
  5. C Witt

    C Witt Guest

    how will that help??.. (take a close look at the 2 lists)
     
    C Witt, Aug 13, 2004
    #5
  6. C Witt

    C Witt Guest

    i still want it to sort the y cord, i just want it to reverse how it
    sorts the x cord at the same time..
     
    C Witt, Aug 13, 2004
    #6
  7. C Witt

    Doug Broad Guest

    One way:
    (defun sort ()
    (mapcar '(lambda(x) (nth x ptlst))
    (vl-sort-i ptlst
    '(lambda (a b)
    (or (< (car a)(car b))
    (and
    (= (car a) (car b))
    (< (cadr a) (cadr b))
    )
    )
    )
    )
    ))

    ;regards Doug
     
    Doug Broad, Aug 13, 2004
    #7
  8. C Witt

    Doug Broad Guest

    That should be:
    (defun sort (ptlst)
    (mapcar '(lambda(x) (nth x ptlst))
    (vl-sort-i ptlst
    '(lambda (a b)
    (or (< (car a)(car b))
    (and
    (= (car a) (car b))
    (< (cadr a) (cadr b))
    )
    )
    )
    )
    ))
     
    Doug Broad, Aug 13, 2004
    #8
  9. change the > to < to sort in reverse,
    looks like you are saying you want the points sorted by the x coordinate in increasing order,
    then for any duplicates, sort the items by Y in decreasing order....

    That would be better andled by sorting the list by X first, then running a loop to go through and resort any duplicates
    by the Y in decreasing order

    I'll write it if the other posts do not solve it...

    C Witt <>
    |>i still want it to sort the y cord, i just want it to reverse how it
    |>sorts the x cord at the same time..
    |>
    |>James Maeding wrote:
    |>
    |>> just change the cadr to whatever element you care about,
    |>> car for x, cadr for y, caddr for z....
    |>>
    |>> C Witt <>
    |>> |>ok.. but one problem..
    |>> |>
    |>> |>(vl-sort '((27.7521 19.0702 0.0)
    |>> |>(27.752 19.0702 0.0)
    |>> |>(27.7519 19.0702 0.0)
    |>> |>(27.7518 19.0702 0.0)
    |>> |>(27.7521 18.0702 0.0))
    |>> |> (function (lambda (e1 e2)
    |>> |> (> (cadr e1) (cadr e2)))))
    |>> |>
    |>> |>returns a list of:
    |>> |>((27.7521 19.0702 0.0)
    |>> |>(27.752 19.0702 0.0)
    |>> |>(27.7519 19.0702 0.0)
    |>> |>(27.7518 19.0702 0.0)
    |>> |>(27.7521 18.0702 0.0))
    |>> |>
    |>> |>when I want a list of:
    |>> |>((27.7518 19.0702 0.0)
    |>> |>(27.7519 19.0702 0.0)
    |>> |>(27.752 19.0702 0.0)
    |>> |>(27.7521 19.0702 0.0)
    |>> |>(27.7521 18.0702 0.0))
    |>>
    |>> James Maeding
    |>> Civil Engineer/Programmer

    James Maeding
    Civil Engineer/Programmer
     
    James Maeding, Aug 13, 2004
    #9
  10. C Witt

    C Witt Guest

    "Looks like you are saying you want the points sorted by the x
    coordinate in increasing order, then for any duplicates, sort the items
    by Y in decreasing order"

    yes, and i will also want another that sorts y in decreasing order, then
    sorts duplicates of y by x in increasing order.. (I can do the latter if
    i have a base to work from)..

    TIA
     
    C Witt, Aug 13, 2004
    #10
  11. C Witt

    Doug Broad Guest

    C.,
    Have you looked down the thread a bit?

    "C Witt" <> wrote in message news:411d010f$1_3@newsprd01...
     
    Doug Broad, Aug 13, 2004
    #11
  12. C Witt

    C Witt Guest

    sry.. hadn't tried that yet.

    i will as soon as i get a few minutes.
     
    C Witt, Aug 13, 2004
    #12
  13. nice, good stuff

    "Doug Broad" <>
    |>That should be:
    |>(defun sort (ptlst)
    |> (mapcar '(lambda(x) (nth x ptlst))
    |> (vl-sort-i ptlst
    |> '(lambda (a b)
    |> (or (< (car a)(car b))
    |> (and
    |> (= (car a) (car b))
    |> (< (cadr a) (cadr b))
    |> )
    |> )
    |> )
    |> )
    |> ))
    |>
    |>|>> One way:
    |>> (defun sort ()
    |>> (mapcar '(lambda(x) (nth x ptlst))
    |>> (vl-sort-i ptlst
    |>> '(lambda (a b)
    |>> (or (< (car a)(car b))
    |>> (and
    |>> (= (car a) (car b))
    |>> (< (cadr a) (cadr b))
    |>> )
    |>> )
    |>> )
    |>> )
    |>> ))
    |>>
    |>> ;regards Doug
    |>|>> > ok.. i have a list of points that I need to sort twice.. once by x
    |>> > cord, and once by y cord.
    |>> >
    |>> > ((12.99999 6.81111 0.0)(4582.61795 3247.13846 0.0)(ect....))
    |>> >
    |>> > you get the idea..
    |>> >
    |>> > x cord will need to be smallest to largest, and y cord will be largest
    |>> > to smallest.
    |>> >
    |>> > now how?
    |>> >
    |>>
    |>>
    |>

    James Maeding
    Civil Engineer/Programmer
     
    James Maeding, Aug 16, 2004
    #13
  14. C Witt

    Doug Broad Guest

    Thanks James.
     
    Doug Broad, Aug 17, 2004
    #14
  15. C Witt

    C Witt Guest

    a long time in comming, but thank you.
     
    C Witt, Sep 14, 2004
    #15
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.