Go Back   Cadalyst Discussion Forums > Forums > Hot Tip Harry: AutoCAD Customization
FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Notices

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  
Old 02-11-2008, 06:29 PM
gidtil gidtil is offline
Junior Member
 
Join Date: Jan 2008
Posts: 14
Default Reorder List Elements

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)
I cannot control the way that the list is created. I was hoping there was an existing routine that already did something like this.

Mylist comes out more similar to this though:
Code:
(1-13 1-07 4-02 1-03 3-06)
I'd Need it to result like this:
Code:
(1-03 1-07 1-13 3-06 4-02)
If you know of an existing function, or even routine, that does this, I would really appreciate your knowledge. Thank you.
  #2  
Old 02-11-2008, 07:27 PM
Harry_Is_Alive's Avatar
Harry_Is_Alive Harry_Is_Alive is offline
Guest
 
Join Date: Jan 2006
Location: Silicon Cornfields, OH
Posts: 277
Default

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  
Old 02-11-2008, 08:12 PM
CarlB CarlB is offline
Senior Member
 
Join Date: Jun 2004
Posts: 667
Default

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  
Old 02-11-2008, 09:36 PM
Harry_Is_Alive's Avatar
Harry_Is_Alive Harry_Is_Alive is offline
Guest
 
Join Date: Jan 2006
Location: Silicon Cornfields, OH
Posts: 277
Default

Quote:
Originally Posted by CarlB
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.
Yup! Here is an example that handles that sort of thing. MAPCAR LAMBDA WARNING - the following code can cause brain damage.

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  
Old 02-12-2008, 07:35 AM
Cap'n Mac's Avatar
Cap'n Mac Cap'n Mac is offline
Senior Member
 
Join Date: May 2002
Location: Bryan, Texas
Posts: 2,195
Default brain hurts (and not from BK's lambda)

Quote:
Originally Posted by gidtil
I cannot control the way that the list is created. I was hoping there was an existing routine that already did something like this.

I'd Need it to result like this:
Code:
(1-03 1-07 1-13 3-06 4-02)
Is there any way you could create the list elements as dotted pairs instead of the hyphenated string?
(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  
Old 02-13-2008, 05:23 PM
gidtil gidtil is offline
Junior Member
 
Join Date: Jan 2008
Posts: 14
Default

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 11:31 PM.





Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.