sorting a list and (cdr list) within

Discussion in 'AutoCAD' started by Marcel Janmaat, Apr 6, 2004.

  1. Has anybody got a good suggestion for me on how to sort this list and the
    (cdr lists) within this list????

    ( ("E_WCD" ("" 33 "") ("SC" 13 ""))
    ("E_ARM" ("" 22 "") ("D" 37 "") ("E" 23 "") ("G" 15 "") ("F" 6 "") ("L" 2
    "") ("B" 22 "symbolen type E_ARM") ("H" 60 "Test") ("A" 14 "") ("I" 5 "")
    ("K" 3 "") ("C" 1 "") ("J" 6 ""))
    ("E_ASP" ("" 24 "") ("T/D" 9 "") ("HD" 4 "") ("OD" 5 "") ("SD" 2 "") ("HT"
    2 "symbolen type E_ASP"))
    ("E_SKL" ("" 37 ""))
    ("E_DET" ("" 46 "2"))
    ("E_SVK" ("KK" 1 "") ("MK" 1 ""))
    ("E_BSP" ("" 2 ""))
    ("E_SGN" ("" 14 "1"))
    ("E_AKS" ("" 4 ""))
    )

    Best regards MJ
     
    Marcel Janmaat, Apr 6, 2004
    #1
  2. Marcel Janmaat

    bob.at Guest

    Marcel,

    try this:

    (setq l1 nil)
    (foreach a l
    (setq lteil (cons (car a) (vl-sort (cdr a) (function (lambda (e1 e2)(< (car e1) (car e2)))))))
    (setq l1 (cons lteil l1))
    )
    (setq l2 (vl-sort l1 (function (lambda (e1 e2)(< (car e1) (car e2))))))


    bob.at
     
    bob.at, Apr 6, 2004
    #2
  3. Thanx Bob,

    It seems to work just fine.

    I only do not exactly understand how you make it happen. Meaning the part
    "(function (lambda (e1 e2) (< (car e1) (car e2))))".
    I read the manual on it but still don't seem to get it.

    Maby you can shed some light?
     
    Marcel Janmaat, Apr 6, 2004
    #3
  4. Marcel Janmaat

    bob.at Guest

    Hello Marcel,

    "function" only tells the lisp compiler that the following ist to treat in similar way as a bult in lisp function. I think, you also can omitt it, but than you cant debug th following function

    "lambda" is a anonymous function, because it is to much afford to make a own defun.
    Instead of
    (vl-sort (cdr a) (function (lambda (e1 e2)(< (car e1) (car e2))))))

    you could write:
    (vl-sort (cdr a) (mysort x1 x2))

    and at an other place:
    (defund mysort (e1 e2 /)
    (< (car e1) (car e2)))

    (though I have not tested this)

    bob.at
     
    bob.at, Apr 6, 2004
    #4
  5. I'm sorry but still don't understand the "(lambda (e1 e2)(< (car e1) (car
    e2)))"
    Lambda = the anonymous function. Ok.
    But i don't understand how "(e1 e2)(< (car e1) (car e2))" sorts the list.

    It must be me.

    Cheers MJ



    similar way as a bult in lisp function. I think, you also can omitt it, but
    than you cant debug th following function
     
    Marcel Janmaat, Apr 7, 2004
    #5
  6. Marcel Janmaat

    bob.at Guest

    The sorting of list is done by vl-sort. vl-sort takes any pair of elements from the list and sends it to the lambda function. If this function returns true, the order of elements is ok, if it returns nil, the order of elements is changed. This is done until all elements are in the correct order.
    If you change th "<" operator in the function to ">" the elements are sorted descending. But you also can define any other function wich returns true or nil. So it is possible to make a sort function wich sorts the characters as "A" "a" "B" "b" etc. and use it as parameter in vl-sort
     
    bob.at, Apr 7, 2004
    #6
  7. Okay Bob, I get it now. Thanx for explaining.

    MJ

    from the list and sends it to the lambda function. If this function returns
    true, the order of elements is ok, if it returns nil, the order of elements
    is changed. This is done until all elements are in the correct order.
    sorted descending. But you also can define any other function wich returns
    true or nil. So it is possible to make a sort function wich sorts the
    characters as "A" "a" "B" "b" etc. and use it as parameter in vl-sort
     
    Marcel Janmaat, Apr 7, 2004
    #7
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.