vl-sort

Hallo,
Bricscad V14 and 15 has a problem with vl-sort and arrays.

(setq lst '((0 1 3) (0 2 3) (0 2 1) (0 2 0) (0 3 3) (0 3 1) (0  2 3) (0 1 3) (0 1 1) (0 1 6)))
(setq lst_sort(vl-sort (vl-sort lst '(lambda (e1 e2) (< (caddr e1) (caddr e2))))'(lambda (e1 e2) (< (cadr e1) (cadr e2)))))

incorrect output with bricscad
((0 1 6) (0 1 1) (0 1 3) (0 1 3) (0 2 0) (0 2 1) (0 2 3) (0 2 3) (0 3 3) (0 3 1))

correct output with Autocad 2002
((0 1 1) (0 1 3) (0 1 3) (0 1 6) (0 2 0) (0 2 1) (0 2 3) (0 2 3) (0 3 1) (0 3 3))

Is there a workaround for this problem?
Christian Köhler

Comments

  • I would use a recursive function to sort the list:
    [code](defun IntListSmallerP (a b)
      (if (and (car a) (= (car a) (car b)))
        (IntListSmallerP (cdr a) (cdr b))
        (< (car a) (car b))
      )
    )[/code]
    [code](setq lst '((0 1 3) (0 2 3) (0 2 1) (0 2 0) (0 3 3) (0 3 1) (0  2 3) (0 1 3) (0 1 1) (0 1 6)))
    (vl-sort lst 'IntListSmallerP) =>
    ((0 1 1) (0 1 3) (0 1 3) (0 1 6) (0 2 0) (0 2 1) (0 2 3) (0 2 3) (0 3 1) (0 3 3))[/code]
  • BTW: The BricsCAD output in the OP is also correct.
  • Thank you, Roy

    BTW: The BricsCAD output in the OP is also correct.

    Why the Bricscad output is also correct?

    With a little change your function is perfect, because the 1. element  in my list is an entityname and 2. und 3. element are sortorder.

    [code] (defun IntListSmallerP (a b)
       (if (and (cadr a) (= (cadr a) (cadr b)))
         (IntListSmallerP (cdr a) (cdr b))
         (< (cadr a) (cadr b))
       )
     )[/code]  numers
    Is this also possible with a list with elementname and 3 numbers?
    lst '((ent 1 3 1) (ent 2 3 2) (ent 2 1 3) (ent 2 0 4) (ent 3 3 5) (ent 3 1 5) (ent  2 3 6) (ent 1 3 7) (ent 1 1 8) (ent 1 6 9)))
    The sortorder is 2. element class 1; 3. element class 2;  4. element class 3, but i can change the order in the list.
    I want export a list with areas and position- numbers from Bricscad to OpenOffice calc. With acad and 32-Bit Bricscad i had a vba- makro with OpenOffice-sort- function. With Vlisp i can export to calc and format the cells but i can´t find a possibility to sort calc- columns with vlisp.
    But the sort-function with lisp is the better solutuion then in Calc.
    Thank you
    Christian

  • Your lisp code does two sorts.  The first sort works on the third value of each triad.  The second sort works on the second value of each triad, but has no code to keep the third elements in the sort order from the first sort.  You are depending on how vl-sort seems to work in the version(s) of Autocad you have rather than having code that fully controls the sorting.  There is no guarantee that the next version of Autocad will behave the same way since this is undocumented.  With your code any result that has the second elements in increasing order is a "correct" output.
  • @ Christian: To skip the first element in the sublists you can do this (note: use original version of IntListSmallerP):
    [code](setq lst '(("A" 0 1 3) ("B" 0 2 3) ("C" 0 2 1) ("D" 0 2 0) ("E" 0 3 3) ("F" 0 3 1) ("G" 0  2 3) ("H" 0 1 3) ("I" 0 1 1) ("J" 0 1 6)))
    (vl-sort lst '(lambda (a b) (IntListSmallerP (cdr a) (cdr b)))) =>
    (("I" 0 1 1) ("A" 0 1 3) ("H" 0 1 3) ("J" 0 1 6) ("D" 0 2 0) ("C" 0 2 1) ("G" 0 2 3) ("B" 0 2 3) ("F" 0 3 1) ("E" 0 3 3))[/code]
  • @ Christian:
    I am interested in knowing how you connect to OpenOffice. I have a problem with the delayed start-up response from OpenOffice (I get a component busy dialog).
    See: http://forum.bricsys.com/discussion/21458
  • @ Christian: Perhaps fixCadTitleblock00.lsp from http://www.theswamp.org/index.php?topic=46634.0 is your work?
  • @ Christian:
    I am interested in knowing how you connect to OpenOffice. I have a problem with the delayed start-up response from OpenOffice (I get a component busy dialog).
    See: http://forum.bricsys.com/discussion/21458


    Hallo, Roy,
    you must wait, button "retry".
    or you start OpenOffice manuell.
    Christian
  • @ Christian: Perhaps fixCadTitleblock00.lsp from http://www.theswamp.org/index.php?topic=46634.0 is your work?


    yes
  • We have fixed implementation of (vl-sort) and (vl-sort-i) ...
    besides a performance improvement by 30%...100%, the main improvement is, that sorting is now identical as AutoCAD AutoLISP does.
    I checked all the samples with data tuples/triples, using multi-stage sorting, and all results are the same ...
    So I'm optimistic that Lisp code using sorting will now be fine :-)
    Greetings & thanks to trigger this topic !
  • @ Christian:
    I am interested in knowing how you connect to OpenOffice. I have a problem with the delayed start-up response from OpenOffice (I get a component busy dialog).
    See: http://forum.bricsys.com/discussion/21458


    Hallo, Roy, auf deutsch kann ich mich besser ausdrücken.
    Calc braucht Zeit zum Start. Wenn die Tabelle nicht sofort genutzt wird, kommt die Fehlermeldung nicht.
    [code]
        (setq oCalc (ooffice_calc_new))
        (setq SS1 (ssget '((2 . "MAS_BL_CK"))))
        (setq oSheet (ooffice_sheet_getactive oCalc))  ;Aufruf erst nach Userauswahl!!!
        ;Kopfzeile als Beispiel für Zugriff auf Calc
        (setq oStyleFamilies (vlax-get oCalc 'StyleFamilies))
        (setq oPageStyles (vlax-invoke-method oStyleFamilies 'getByName "PageStyles"))
        (setq oDefPage (vlax-invoke-method PageStyles 'getByName "Default"))
        (vlax-put-property oDefPage "HeaderIsOn" :vlax-true)   
        (setq HContent (vlax-get-property oDefPage "RightPageHeaderContent"))
        (setq HText (vlax-get-property HContent "CenterText"))
        (vlax-invoke-method HText 'setString (strcat "Zeile 1" (Chr 13) "Zeile 2"))
        (vlax-put-property oDefPage "RightPageHeaderContent" HContent)
    [/code]
    Vielen Dank auch an Torsten Moses.
    Christian
  • @ Christian:
    I already have 'ActiveX delay' issues when a new file is opened.
    But meanwhile I have come up with a solution. I have posted it here:
    http://forum.bricsys.com/discussion/21458
  • Having the vl-sort in Bricscad updated to match vl-sort in Autocad is a good thing, but I strongly recommend against assuming that vl-sort will always behave as it does today.  It is very easy to configure a vl-sort conditional statement that will always provide the output you want.  Many of us who program for Autocad have had instances where code that worked properly in one release would give different output in a newer release because Autodesk had "improved" a function. 
This discussion has been closed.