See the top rated post in this thread. Click here

Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Need vl-sort to numerically sort the following...

  1. #11
    Active Member
    Join Date
    2012-06
    Posts
    96
    Login to Give a bone
    0

    Default Re: Need vl-sort to numerically sort the following...

    I tried to insert the "<-- fill out table here" code (see code below) and it gives me an error ** Error: bad argument type: consp 4 **

    Code:
    (if l
        (progn
          (setq hgt (if (zerop (cdr (assoc 40 (setq e (entget (tblobjname "STYLE" "UNIT_COUNT"))))))
    		  (* (getvar 'textsize) 2.0)
    		  (* (cdr (assoc 40 e)) 2.0)))
          (setq columns 2)
          (setq r 2)
          (if (setq ins (list 25.5405 4.12052 0.0))
    	(progn
    	  (setq tbl (vla-addtable
    		      (vlax-get
    			(vla-get-activelayout
    			  (vla-get-activedocument (vlax-get-acad-object))) 'BLOCK)
    		      (vlax-3d-point ins)
    		      (+ (length l) 2)
    		      columns
    		      0.05 ;(* hgt 1.5)
    		      0.05 ;(* hgt 1.5)
    		      )
    		)
    	  (setq inc -1)
    	  (repeat 2
    	    (vla-setcolumnwidth tbl (setq inc (1+ inc)) (* hgt 4.0)))
    	  	  
              (vla-setrowheight tbl 0 0.05)
    	  (vla-setrowheight tbl 1 0.05)	  
              (vla-settext tbl 0 0 (strcat "Unit Total"))	  
              (vla-setcolumnwidth tbl 0 (* hgt 5.0))
    	  (vla-setcolumnwidth tbl 1 (* hgt 5.0))
    	  (vla-settext tbl 1 0 "Unit")
    	  (vla-settext tbl 1 1 "QTY")))
          (defun WriteAndSet (col row string)
    	(vla-settext tbl row col string)
    	(vla-setcellalignment tbl row col acMiddleCenter))
          
          (foreach x l
    	(WriteAndSet 0 r (car x))
    	(WriteAndSet 1 r (cadr x))
    	(setq r (1+ r)))
          )
        )
    I have attached the drawing which you could use as testing environment, that has the "unit" blocks and the table. If you think it would be relevant for me to include the code I have to create units please let me know.
    Attached Files Attached Files
    Last edited by vladimir.karatsupa982899; 2014-10-17 at 07:24 PM. Reason: Added clarification

  2. #12
    Active Member
    Join Date
    2012-06
    Posts
    96
    Login to Give a bone
    0

    Default Re: Need vl-sort to numerically sort the following...

    Any Ideas on how to fix the error?

  3. #13
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    0

    Default Re: Need vl-sort to numerically sort the following...

    Just to address the original question.

    P=

    Code:
    (defun C:Test (/ lstOfSublists)
     (setq lstOfSublists '(("1-302" 2) ("2-129" 4) ("2-11" 6) ("1-8" 3) ("2-1" 7)("2-100" 9)))
     (sortlistofsublistsbyitem lstOfSublists 0)
    )
    
    (defun sortListofSublistsbyItem (lstOfSublists intItem)
     (vl-sort lstOfSublists '(lambda (X Y) (< (nth intItem X) (nth intItem Y))))
    )
    AutomateCAD

  4. #14
    Active Member
    Join Date
    2012-06
    Posts
    96
    Login to Give a bone
    0

    Default Re: Need vl-sort to numerically sort the following...

    Peter, this is what I get when I try it on my side...(see below)
    Code:
    Command: TEST
    (("1-302" 2) ("1-8" 3) ("2-1" 7) ("2-100" 9) ("2-11" 6) ("2-129" 4))
    Command:
    I need the ("1-8" 3) to come before ("1-302" 2) likewise ("2-11" 6) to come before ("2-100" 9)

    Any suggestions?

  5. #15
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    1

    Default Re: Need vl-sort to numerically sort the following...

    Not difficult.

    P=

    Code:
    ;___________________________________________________________________________________________________________ 
    ;
    ; Test Function to sort a list of strings separated by a dash as numbers
    ; Written By: Peter Jamtgaard Copyright 2015 All rights reserved
    ;___________________________________________________________________________________________________________
    
    (defun C:Test (/ lstOfSublists)
     (if (and
          (setq lstOfSublists '(("1-302" 2) ("2-129" 4) ("2-11" 6) ("1-8" 3) ("2-1" 7)("2-100" 9)))
          (setq lstOfSublists (mapcar 'listprefix lstOfSublists))
          (setq lstOfSublists (sortlistofsublistsbyitem lstOfSublists 1))
          (setq lstOfSublists (sortlistofsublistsbyitem lstOfSublists 0))
          (setq lstOfSublists (mapcar 'cddr lstOfSublists))
         )
      lstOfSublists
     )
    )
    
    (defun ListPrefix (lstSublist)
     (append (numberConvert (car lstSublist)) lstSublist)
    )
    
    (defun NumberConvert (strItem)
     (mapcar 'atoi (csvstringtolist strItem "-"))
    )
    
    ;___________________________________________________________________________________________________________ 
    ;
    ; Function to sort a list of sublists by item
    ;___________________________________________________________________________________________________________
    
    
    (defun sortListofSublistsbyItem (lstOfSublists intItem)
     (vl-sort lstOfSublists '(lambda (X Y) (< (nth intItem X) (nth intItem Y))))
    )
    
    ;___________________________________________________________________________________________________________ 
    ;
    ; Function to convert a csv string to a list
    ;___________________________________________________________________________________________________________
    
    
    (defun CSVStringToList  (strText strChar / intPosition lstStrings)
     (while (setq intPosition (vl-string-search strChar strText 0))
      (setq lstStrings  (cons (substr strText 1 intPosition) lstStrings)
            strText     (substr strText (+ intPosition 1 (strlen strChar)))
      )
     )
     (if lstStrings
      (reverse (cons strText lstStrings))
      (list strText)
     )
    )
    (vl-load-com)
    AutomateCAD

  6. #16
    Active Member
    Join Date
    2012-06
    Posts
    96
    Login to Give a bone
    0

    Default Re: Need vl-sort to numerically sort the following...

    YYYYEEEESSSS!

    It worked thank you very much Peter. Of course it'l probably take me months to understand your code, but it does work!!!!! Thank you

  7. #17
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    2

    Default Re: Need vl-sort to numerically sort the following...

    It won't take months.

    It is actually very straight forward

    Here is your list of sublists

    (setq lstOfSublists '(("1-302" 2) ("2-129" 4) ("2-11" 6) ("1-8" 3) ("2-1" 7)("2-100" 9)))

    For anyone who is not comfortable with MAPCAR it means
    Multiple Applications to the Contents of the Address Register

    In English it means

    Do this function to each member of this list.

    So...

    (setq lstOfSublists (mapcar 'listprefix lstOfSublists))

    Means run LISTPREFIX on each sublist in your list of sublists.

    So listprefix...

    (defun ListPrefix (lstSublist)
    (append (numberConvert (car lstSublist)) lstSublist)
    )

    Runs NUMBERCONVERT on each sublist and appends it to the beginning of sublist

    So NumberConvert

    (defun NumberConvert (strItem)
    (mapcar 'atoi (csvstringtolist strItem "-"))
    )

    The first item in the list is "1-302" converts to '("1" "302") using the csvstringtolist function
    (with the "-" being the delimiter) (CSV is comma [or in this case dash] delimited value) and then
    Mapcar 'atoi converts '("1" "302") to '(1 302) ... Do the ATOI function to each member of the list

    So number convert actually converts

    ("1-302" 2) to ( 1 302 "1-302" 2)

    SortlistOfSublistsByItem sorts the list by the 1 item and then the 0 item.

    Finally it mapcar 'CDDR the list of sublists and converts then back in to the original

    '( ("1-8" 3)...)

    Done

    Hope that helps for you to understand the process I used.
    Last edited by peter; 2015-02-05 at 06:24 AM.
    AutomateCAD

  8. #18
    Active Member
    Join Date
    2012-06
    Posts
    96
    Login to Give a bone
    0

    Default Re: Need vl-sort to numerically sort the following...

    It makes perfect sense now, thank you for taking your time to explain to a novice like myself.

    Thank you

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Sort anything and everything
    By pauljordan in forum NavisWorks - Wish List
    Replies: 0
    Last Post: 2011-06-29, 12:38 AM
  2. Array Sort
    By BeetleJuice in forum VBA/COM Interop
    Replies: 4
    Last Post: 2009-12-15, 08:35 PM
  3. Program to sort objects in a list numerically
    By ccowgill in forum AutoLISP
    Replies: 8
    Last Post: 2008-02-04, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •