Jump to content

Recommended Posts

Posted

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?

Posted
(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))))
            )
          )
)

 

  • Like 1
Posted

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")

 

  • Like 1

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...