Jump to content

List sorting- problem


muthu123

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...