Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sort plus min max

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Anonymous
847 Views, 3 Replies

Sort plus min max

Anonymous
Not applicable

Hi!

I have a little challenging problem. I have this list:

(setq mylist 
  (list
    (a (25 3))
    (b (30 8))
    (a (60 8))
    (a (80 5))
    (b (50 7))
    (c (14 2))
  )
)

Where the first element is the name of a place and the second one is their coordinates.Now I need the middle point for each group of letter at the first element and the biggest one of the second member in the second place. I mean:

(a (52.5 8))
(b (40 8))
(c (14 2))

I can't find any easy way to accomplish this simple task. Every help is welcome!

Best regards

0 Likes

Sort plus min max

Hi!

I have a little challenging problem. I have this list:

(setq mylist 
  (list
    (a (25 3))
    (b (30 8))
    (a (60 8))
    (a (80 5))
    (b (50 7))
    (c (14 2))
  )
)

Where the first element is the name of a place and the second one is their coordinates.Now I need the middle point for each group of letter at the first element and the biggest one of the second member in the second place. I mean:

(a (52.5 8))
(b (40 8))
(c (14 2))

I can't find any easy way to accomplish this simple task. Every help is welcome!

Best regards

3 REPLIES 3
Message 2 of 4
rkmcswain
in reply to: Anonymous

rkmcswain
Mentor
Mentor
 I need the middle point for each group of letter at the first element and the biggest one of the second member in the second place

The data in your example doesn't match anything in the list above that. So you want to sort by the first number in the number pair, and in case of the same number, use the second number to sort?

R.K. McSwain     | CADpanacea | on twitter
0 Likes

 I need the middle point for each group of letter at the first element and the biggest one of the second member in the second place

The data in your example doesn't match anything in the list above that. So you want to sort by the first number in the number pair, and in case of the same number, use the second number to sort?

R.K. McSwain     | CADpanacea | on twitter
Message 3 of 4
ВeekeeCZ
in reply to: Anonymous

ВeekeeCZ
Consultant
Consultant
Accepted solution

Not sure if this is simple way, but it works...

 

(defun :sort-by-letter-mid-max (lst / l a)
  (foreach e lst
    (setq l (if (setq a (assoc (car e) l))
	      (subst (append a (cdr e)) a l)
	      (cons e l))))
  
  (vl-sort (mapcar '(lambda (x)
		      (list (car x)
			    (list (/ (apply '+ (mapcar 'car (cdr x)))
				     (- (length x) 1.))
				  (apply 'max (mapcar 'cadr (cdr x))))))
		   l)
	   '(lambda (a b) (< (car a) (car b)))))

For 

(setq mylist '(("a" (25 3))
	       ("b" (30 8))
	       ("a" (60 8))
	       ("a" (80 5))
	       ("b" (50 7))
	       ("c" (14 2))))

result is:

(:sort-by-letter-mid-max mylist)
(("a" (55.0 8)) ("b" (40.0 8)) ("c" (14.0 2)))

 

0 Likes

Not sure if this is simple way, but it works...

 

(defun :sort-by-letter-mid-max (lst / l a)
  (foreach e lst
    (setq l (if (setq a (assoc (car e) l))
	      (subst (append a (cdr e)) a l)
	      (cons e l))))
  
  (vl-sort (mapcar '(lambda (x)
		      (list (car x)
			    (list (/ (apply '+ (mapcar 'car (cdr x)))
				     (- (length x) 1.))
				  (apply 'max (mapcar 'cadr (cdr x))))))
		   l)
	   '(lambda (a b) (< (car a) (car b)))))

For 

(setq mylist '(("a" (25 3))
	       ("b" (30 8))
	       ("a" (60 8))
	       ("a" (80 5))
	       ("b" (50 7))
	       ("c" (14 2))))

result is:

(:sort-by-letter-mid-max mylist)
(("a" (55.0 8)) ("b" (40.0 8)) ("c" (14.0 2)))

 

Message 4 of 4
john.uhden
in reply to: ВeekeeCZ

john.uhden
Mentor
Mentor

vl-symbol-name?

John F. Uhden

0 Likes

vl-symbol-name?

John F. Uhden

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report