rog1n Posted August 2, 2019 Share 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 Link to comment Share on other sites More sharing options...
Tharwat Posted August 2, 2019 Share 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 Link to comment Share on other sites More sharing options...
rog1n Posted August 2, 2019 Author Share Posted August 2, 2019 Thank you Tharwat! Quote Link to comment Share on other sites More sharing options...
Tharwat Posted August 2, 2019 Share Posted August 2, 2019 You're welcome anytime. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted August 2, 2019 Share 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 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.