Sort plus min max

Anonymous

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
Reply
Accepted solutions (1)
899 Views
3 Replies
Replies (3)

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

В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

john.uhden
Mentor
Mentor

vl-symbol-name?

John F. Uhden

0 Likes