TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: civil.eng on July 05, 2021, 09:07:12 AM
-
Hi all,
I need a function to sort a list by first element , like this :
(("3" Xc Yc) ("1" Xc Yc) ("2" Xc Yc)) >> (("1" Xc Yc) ("2" Xc Yc) ("3" Xc Yc))
(("003" Xc Yc) ("001" Xc Yc) ("002" Xc Yc)) >> (("001" Xc Yc) ("002" Xc Yc) ("003" Xc Yc))
(("A3" Xc Yc) ("A1" Xc Yc) ("A2" Xc Yc)) >> (("A1" Xc Yc) ("A2" Xc Yc) ("A3" Xc Yc))
(("B" Xc Yc) ("C" Xc Yc) ("A" Xc Yc)) >> (("A" Xc Yc) ("B" Xc Yc) ("C" Xc Yc))
I mean the function should work on the above lists, Xc and Yc are center point of a polyline.
I'm using this code to sort :
(vl-sort
lst
'(lambda (x y)
(< (atoi (car x))
(atoi (car y))
)
)
)
But the code only works on the first and the second lists.
-
Have you tried, just :
(vl-sort lst '(lambda ( a b ) (< (car a) (car b))))
-
Yes I have, but the numbers sorted like this :
"1" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "2" "20" "3"
"p1" "p10" "p11" "p12" "p13" "p14" "p2" "p3" "p4" "p4" "p5" "p6" "p7" "p8" "p9"
-
I'd rather change initial list, than change sorting algorithm - still you can search for Lee Mac's (LM:alphanumsort) function and use it...
-
;; Alphanumerical Sort - Lee Mac
;; Sorts a list of strings containing a combination of alphabetical & numerical characters.
(defun LM:alphanumsort (lst)
(mapcar (function (lambda (n) (nth n lst)))
(vl-sort-i (mapcar 'LM:splitstring lst)
(function (lambda (a b / x y)
(while (and (setq x (car a)) (setq y (car b)) (= x y))
(setq a (cdr a)
b (cdr b)
)
)
(cond ((null x) b)
((null y) nil)
((and (numberp x) (numberp y)) (< x y))
((numberp x))
((numberp y) nil)
((< x y))
)
)
)
)
)
)
;; Split String - Lee Mac
;; Splits a string into a list of text and numbers
(defun LM:splitstring (str)
((lambda (l)
(read
(strcat "("
(vl-list->string
(apply 'append
(mapcar (function
(lambda (a b c)
(cond ((= 92 b) (list 32 34 92 b 34 32))
((or (< 47 b 58) (and (= 45 b) (< 47 c 58) (not (< 47 a 58))) (and (= 46 b) (< 47 a 58) (< 47 c 58)))
(list b)
)
((list 32 34 b 34 32))
)
)
)
(cons nil l)
l
(append (cdr l) '(()))
)
)
)
")"
)
)
)
(vl-string->list str)
)
)