muthu123 Posted June 14, 2010 Share Posted June 14, 2010 Dear friends, How can i sort this list ("RF-1" "RF1-10C" "RF1-2" "RF1-2A" "RF1-5" "RF2-10" "RF2-10A" "RF2-2B" "RF2-2C") into ("RF-1" "RF1-2" "RF1-2A" "RF1-5" "RF1-10C" "RF2-2B" "RF2-2C" "RF2-10" "RF2-10A") Yours Muthu Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 14, 2010 Share Posted June 14, 2010 I collaborated with Gile a while back writing this: (defun ArchSort ( lst / SplitStr comparable ) ;; Gile & Lee Mac (defun SplitStr ( str / lst test rslt num tmp ) (setq lst (vl-string->list str) test (chr (car lst))) (if (< 47 (car lst) 58) (setq num T) ) (while (setq lst (cdr lst)) (if num (cond ( (= 46 (car lst)) (if (and (cadr lst) (setq tmp (strcat "0." (chr (cadr lst)))) (numberp (read tmp))) (setq rslt (cons (read test) rslt) test tmp lst (cdr lst)) (setq rslt (cons (read test) rslt) test "." num nil) ) ) ( (< 47 (car lst) 58) (setq test (strcat test (chr (car lst)))) ) (T (setq rslt (cons (read test) rslt) test (chr (car lst)) num nil) ) ) (if (< 47 (car lst) 58) (setq rslt (cons test rslt) test (chr (car lst)) num T) (setq test (strcat test (chr (car lst)))) ) ) ) (if num (setq rslt (cons (read test) rslt)) (setq rslt (cons test rslt)) ) (reverse rslt) ) (defun comparable ( e1 e2 ) (or (and (numberp e1) (numberp e2)) (= 'STR (type e1) (type e2)) (not e1) (not e2) ) ) (mapcar (function (lambda ( x ) (nth x lst))) (vl-sort-i (mapcar (function SplitStr) lst) (function (lambda ( x1 x2 / n1 n2 comp ) (while (and (setq comp (comparable (setq n1 (car x1)) (setq n2 (car x2)))) (= n1 n2) (setq x1 (cdr x1) x2 (cdr x2)) ) ) (if comp (< n1 n2) (numberp n1)) ) ) ) ) ) Quote Link to comment Share on other sites More sharing options...
muthu123 Posted June 14, 2010 Author Share Posted June 14, 2010 I collaborated with Gile a while back writing this: (defun ArchSort ( lst / SplitStr comparable ) ;; Gile & Lee Mac (defun SplitStr ( str / lst test rslt num tmp ) (setq lst (vl-string->list str) test (chr (car lst))) (if (< 47 (car lst) 58) (setq num T) ) (while (setq lst (cdr lst)) (if num (cond ( (= 46 (car lst)) (if (and (cadr lst) (setq tmp (strcat "0." (chr (cadr lst)))) (numberp (read tmp))) (setq rslt (cons (read test) rslt) test tmp lst (cdr lst)) (setq rslt (cons (read test) rslt) test "." num nil) ) ) ( (< 47 (car lst) 58) (setq test (strcat test (chr (car lst)))) ) (T (setq rslt (cons (read test) rslt) test (chr (car lst)) num nil) ) ) (if (< 47 (car lst) 58) (setq rslt (cons test rslt) test (chr (car lst)) num T) (setq test (strcat test (chr (car lst)))) ) ) ) (if num (setq rslt (cons (read test) rslt)) (setq rslt (cons test rslt)) ) (reverse rslt) ) (defun comparable ( e1 e2 ) (or (and (numberp e1) (numberp e2)) (= 'STR (type e1) (type e2)) (not e1) (not e2) ) ) (mapcar (function (lambda ( x ) (nth x lst))) (vl-sort-i (mapcar (function SplitStr) lst) (function (lambda ( x1 x2 / n1 n2 comp ) (while (and (setq comp (comparable (setq n1 (car x1)) (setq n2 (car x2)))) (= n1 n2) (setq x1 (cdr x1) x2 (cdr x2)) ) ) (if comp (< n1 n2) (numberp n1)) ) ) ) ) ) Thank you so much. But the result will be ("RF1-2" "RF1-2A" "RF1-5" "RF1-10C" "RF2-2B" "RF2-2C" "RF2-10" "RF2-10A" "RF-1"). It is better it will be like this ("RF-1" "RF1-2" "RF1-2A" "RF1-5" "RF1-10C" "RF2-2B" "RF2-2C" "RF2-10" "RF2-10A") Thank you. Yours, Muthu Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 14, 2010 Share Posted June 14, 2010 Yes, of course as: (acad_strlsort '("RF-" "RF")) ==>> ("RF" "RF-") Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.