Sortlist

Discussion in 'AutoCAD' started by Marcel Janmaat, Jan 18, 2005.

  1. If i have a somewhat list like this;

    ( ("" "" "")
    ("DT" "DETAIL TEKENING" "DETAIL DRAWING")
    ("BT" "BESTEKTEKENING" "TENDER DRAWING")
    ("MA" "MILIEU AANVRAAG" "ENVIRONMENTAL APPROVAL")
    ("MT" "MILIEU TEKENING" "ENVIRONMENTAL DRAWING")
    ("GV" "GEBRUIKERSVERGUNNING" "USERS APPROVAL")
    ("BT" "CONTRACTSTUK" "CONTRACT DRAWING")
    )

    How can i sort this correctly!

    It is posible that the first item in each sublist could apear several times
    in this list.

    At first I used this,

    (defun h_sortlist (lst / itm srt new)
    (setq srt '())
    (foreach itm lst (if (car itm) (setq srt (append srt (list (car itm))))))
    (setq srt (acad_strlsort srt) new '())
    (foreach itm srt (setq new (append new (list (assoc itm lst)))))
    )

    But it fails with the assoc beceause "BT" apears twice.

    M
     
    Marcel Janmaat, Jan 18, 2005
    #1
  2. The result has to look like this;

    ( ("" "" "")
    ("BT" "BESTEKTEKENING" "TENDER DRAWING")
    ("BT" "CONTRACTSTUK" "CONTRACT DRAWING")
    ("DT" "DETAIL TEKENING" "DETAIL DRAWING")
    ("GV" "GEBRUIKERSVERGUNNING" "USERS APPROVAL")
    ("MA" "MILIEU AANVRAAG" "ENVIRONMENTAL APPROVAL")
    ("MT" "MILIEU TEKENING" "ENVIRONMENTAL DRAWING")
    )
     
    Marcel Janmaat, Jan 18, 2005
    #2
  3. Marcel Janmaat

    Doug Broad Guest

    Marcel,
    For your particular list vl-sort works fine. You should be aware, however,
    that for some lists, vl-sort removes duplicates.

    Assuming that your list is stored in variable b:

    (vl-sort b '(lambda (a b)(< (car a)(car b))))
     
    Doug Broad, Jan 18, 2005
    #3
  4. Marcel Janmaat

    Fatty Guest

    Of course, if you take vl-sort function then ...
    "duplicate elements may be eliminated from the list."
    There are many ways to do this,
    it is somewhat brutal but should work.

    (setq nth_lst (vl-sort-i
    <your list>
    (function (lambda (e1 e2)
    (< (car e1)(car e2))))))
    (setq ret_list
    (mapcar (function (lambda (x)
    (nth x <your list>)))
    nth_lst))
     
    Fatty, Jan 18, 2005
    #4
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.
Similar Threads
There are no similar threads yet.
Loading...