rog1n Posted August 2, 2019 Posted August 2, 2019 Hello, I have a list like: (setq lst '("ABC-ABD.002" "ABD.003" "ABD.001" "ABD.110" "ABC-ABD.050")) I want to sort this list by the number at the end of string, I tried with: (acad_strlsort lst) results: ("ABC-ABD.002" "ABC-ABD.050" "ABD.001" "ABD.003" "ABD.110") but the correct for me is: ("ABD.001" "ABC-ABD.002" "ABD.003" "ABC-ABD.050" "ABD.110") I know I need to break each string in two parts (letters and numbers), but my knowledge is small to break the string then sort and put it all together again. Can anyone help me? Quote
Tharwat Posted August 2, 2019 Posted August 2, 2019 (setq lst '("ABC-ABD.002" "ABD.003" "ABD.001" "ABD.110" "ABC-ABD.050")) (vl-sort lst '(lambda (j k) (< (read (substr j (+ 2 (vl-string-search "." j)))) (read (substr k (+ 2 (vl-string-search "." k)))) ) ) ) 1 Quote
Lee Mac Posted August 2, 2019 Posted August 2, 2019 Another, assuming your strings conform to the format of ending with 3 digits - (defun foo ( l ) (mapcar '(lambda ( n ) (nth n l)) (vl-sort-i (mapcar '(lambda ( x ) (atoi (substr x (- (strlen x) 2)))) l) '<) ) ) _$ (foo '("ABC-ABD.002" "ABD.003" "ABD.001" "ABD.110" "ABC-ABD.050")) ("ABD.001" "ABC-ABD.002" "ABD.003" "ABC-ABD.050" "ABD.110") 1 Quote
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.