|
|||||||
| Hot Tip Harry: AutoCAD Customization Cadalyst's popular Hot Tip Harry and his entourage are here to assist you with AutoCAD customization. Request help with a programming problem, locate a needed routine, or just keep up with Harry's latest activities. You'll find Harry's archive of AutoLISP and VBA code and hatch patterns at www.cadalyst.com/cadtips. Moderated by R.K. McSwain. |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Is there an existing function that can reorder elements within a list. For Example,
Code:
(setq alist (list 5 7 6 1 3 8 9 4 2 0)) (5 7 6 1 3 8 9 4 2 0) (function alist) (0 1 2 3 4 5 6 7 8 9) Mylist comes out more similar to this though: Code:
(1-13 1-07 4-02 1-03 3-06) Code:
(1-03 1-07 1-13 3-06 4-02) |
|
#2
|
||||
|
||||
|
You want to look at (VL-SORT) and (VL-SORT-I) in Visual LISP.
(setq A '(9 2 7 3 5 8)) (setq B (vl-sort A '>)) B is set to (9 8 7 5 3 2) Keep on programmin' Hot Tip Harry (Bill Kramer) |
|
#3
|
|||
|
|||
|
I tried vl-sort on your list and it choked on the number format. it did however handle sort the numbers as text.
(vl-sort '("1-13" "1-07" "4-02" "1-03" "3-06") '<) resulted in: ("1-03" "1-07" "1-13" "3-06" "4-02") so you may need to do some string/number manipulation. |
|
#4
|
||||
|
||||
|
Quote:
Code:
;;;--------------------------------------------------------------------
;;; ParseNumbs - returns list of integers parsed out of an input
;;; string. Quite handy for date parsing and when
;;; working with plan set sheet references.
;;;
(defun ParseNumbs (S / C Buf Res)
(setq Buf "")
(while (> (strlen S) 0)
(setq C (substr S 1 1)
S (substr S 2))
(if (<= (ascii "0") (ascii C) (ascii "9"))
(setq Buf (strcat Buf C))
(if (/= Buf "")
(setq Res (cons Buf Res)
Buf ""))))
(if (/= Buf "") (setq Res (cons Buf Res)))
(mapcar 'atoi (reverse Res))
)
;;;--------------------------------------------------------------------
;;; Sorts numbers like "01-02" by first number then second.
;;;
(defun SortWierdFormat (InList)
(setq sList (mapcar 'ParseNumbs InList)
sList (vl-sort-i sList '(lambda (A B)
(cond
((< (car A) (car B)) T)
((> (car A) (car B)) nil)
((< (cadr A) (cadr B)) T)
(T nil))))
)
(mapcar '(lambda (A) (nth A InList)) sList))
;;;--------------------------------------------------------------------
(defun C:TEST ()
(setq A '("01-12" "04-01" "01-10" "03-02" "04-05"))
(print A)
(sortWierdFormat A)
)
;;;----------------------------------------- Keep on programmin'
![]() Bwuah ha ha ha ha ha !!! (added Mad Scientist Laugh to code) Hot Tip Harry (Bill Kramer) Last edited by Harry_Is_Alive; 02-12-2008 at 09:44 AM. |
|
#5
|
||||
|
||||
|
Quote:
(cons n mm)(cons n mm), etc. instead of "n-mm" ? Creating the list from lists lets us sort things in a much simpler sort of way. There are so many more tools available for list comparison, which are then more elegant in application (and less likely to make my brain hurt, as all string parsing functions seem to--BTDT, don't want the t-sheirt never no more). Taking the list "apart" afterwards is not too terrible difficult, either. Just a thought. |
|
#6
|
|||
|
|||
|
Wow thanks guys for your input.
The output of the list I have no way to control. It extracts Tags from existing Block References and the Tags are #-## the numbers are not seperate the dash is not created by the routine. I have no problem with taking the list "apart" though. However Harry's Code seems to work beautifully so I don't think that its much of an issue. I cannot believe I missed vl-sort an vl-sort-i. I use AidaCAD for my function reference and its in there. Thanks for the tip and the code! |
| Thread Tools | |
| Display Modes | |
|
|