merge lists and sort by numbers

merge lists and sort by numbers

sunlight1
Enthusiast Enthusiast
557 Views
5 Replies
Message 1 of 6

merge lists and sort by numbers

sunlight1
Enthusiast
Enthusiast

HI 

i need help to organize and merge 2 lists. 

for example i have list 1   (3 1 7 5 9)

and list A                            (C K R A T)

i need to merge them together, so 3 must come with C , and 1 must come with K etc...

for example (3C 1K 7R 5A 9T) 

and then  i want to sort them by numbers.   so (print lists) will be (1K 3C 5A 7R 9T)

and (car lists) will be (1K)

P.S  I actually have dates and names but it's simpler that way

 

tnx for helping

0 Likes
Accepted solutions (1)
558 Views
5 Replies
Replies (5)
Message 2 of 6

ВeekeeCZ
Consultant
Consultant
Accepted solution

(3 1 7 5 9) are strings or integers?

(C K R A T) are strings, right? '("C" "K" ...)

 

anyways...

(defun :sortmerge (nlst alst)
  (mapcar '(lambda (l) (strcat (vl-princ-to-string (car l)) (cdr l)))
	  (vl-sort (mapcar 'cons nlst alst) '(lambda (n1 n2) (< (car n1) (car n2))))))


> (:sortmerge '(3 1 7 5 9) '("C" "K" "R" "A" "T"))
> ("1K" "3C" "5A" "7R" "9T")
or...
> (:sortmerge '("3" "1" "7" "5" "9") '("C" "K" "R" "A" "T"))
> ("1K" "3C" "5A" "7R" "9T")

 

0 Likes
Message 3 of 6

martti.halminen
Collaborator
Collaborator

The easiest way probably is first sorting the numbers using VL-SORT-I, and then using MAPCAR and NTH on the index file to pick the items to combine.

0 Likes
Message 4 of 6

sunlight1
Enthusiast
Enthusiast

tnx a lot ! working great 

0 Likes
Message 5 of 6

yangguoshe
Advocate
Advocate

(setq m'("3" "1" "7" "5" "9" ) n'("C" "K" "R" "A" "T" ))     

(vl-sort(mapcar'strcat m n)'<)

return:  ("1K" "3C" "5A" "7R" "9T")

Message 6 of 6

calderg1000
Mentor
Mentor

Regards @sunlight1 

Try this...

 

(setq l1 '(3 1 7 5 9))

(setq l2 '(C K R A T))

(setq l12 (mapcar '(lambda(x y) (strcat (itoa x) (vl-symbol-name y))) l1 l2))

(setq lf (vl-sort l12 '(lambda(x y) (< x y))))

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes