Jump to content

Recommended Posts

Posted

Good catch Lee! Improved code below.

; (KGA_String_SliceChrSet "abc123cde456fgh789" "0123456789") => ("abc" "123" "cde" "456" "fgh" "789")
(defun KGA_String_SliceChrSet (str chrSet / ret sub)
 (setq chrSet (vl-string->list chrSet))
 (mapcar
   '(lambda (cur nxt)
     (if
       (or
         (not nxt)
         (vl-position
           (list (and (vl-position cur chrSet)) (and (vl-position nxt chrSet)))
           '((T nil) (nil T))
         )
       )
       (progn
         (setq ret (cons (vl-list->string (reverse (cons cur sub))) ret))
         (setq sub nil)
       )
       (setq sub (cons cur sub))
     )
   )
   (setq str (vl-string->list str))
   (append (cdr str) '(nil))
 )
 (reverse ret)
)

(defun MySort (lst)
 (setq lst
   (mapcar
     '(lambda (sub) (list (KGA_String_SliceChrSet (car sub) "0123456789") sub))
     lst
   )
 )
 (mapcar
   'cadr
   (vl-sort
     lst
     '(lambda (a b)
       (setq a (car a))
       (setq b (car b))
       (while
         (and
           (car a)
           (car b)
           (= (car a) (car b))
         )
         (setq a (cdr a))
         (setq b (cdr b))
       )
       (if
         (and
           (car a)
           (car b)
           (wcmatch (car a) "#*")
           (wcmatch (car b) "#*")
         )
         (< (atoi (car a)) (atoi (car b)))
         (< (car a) (car b))
       )
     )
   )
 )
)

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • marko_ribar

    8

  • Lee Mac

    7

  • Roy_043

    5

  • Kowal

    4

Posted

Just to inform... Previously posted code in post #19 was unreliable... I revised it again, although result is the same - IMO correct... So if you wish, any further testing is appreciated...

 

M.R.

Posted

@ Lee:

Your function LM:splitstring already 'escapes' the "\\" character. For obvious reasons it should also escape the "\"" character.

Posted
Your function LM:splitstring already 'escapes' the "\\" character. For obvious reasons it should also escape the "\"" character.

 

As you may have gathered, the original function was developed for the purpose of sorting filenames in which such character could not occur, but for general purpose this character should indeed be escaped:

(defun mysort ( lst )
   (mapcar '(lambda ( n ) (nth n lst)) (LM:alphanumsort-i (mapcar 'car lst)))
)

;; Alphanumerical Sort-i  -  Lee Mac
;; Sorts a list of strings containing a combination of alphabetical & numerical characters and returns the indices.

(defun LM:alphanumsort-i ( lst )
   (vl-sort-i (mapcar 'LM:splitstring lst)
       (function
           (lambda ( a b / x y )
               (while
                   (and
                       (setq x (car a))
                       (setq y (car b))
                       (= x y)
                   )
                   (setq a (cdr a)
                         b (cdr b)
                   )
               )
               (cond
                   (   (null x) b)
                   (   (null y) nil)
                   (   (and (numberp x) (numberp y)) (< x y))
                   (   (numberp x))
                   (   (numberp y) nil)
                   (   (< x y))
               )
           )
       )
   )
)

;; Split String  -  Lee Mac
;; Splits a string into a list of text and numbers

(defun LM:splitstring ( str )
   (
       (lambda ( l )
           (read
               (strcat "("
                   (vl-list->string
                       (apply 'append
                           (mapcar
                               (function
                                   (lambda ( a b c )
                                       (cond
                                           (   (or (= 34 b) (= 92 b))
                                               (list 32 34 92 b 34 32)
                                           )
                                           (   (or (< 47 b 58)
                                                  ;(and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
                                                   (and (= 46 b) (< 47 a 58) (< 47 c 58))
                                               )
                                               (list b)
                                           )
                                           (   (list 32 34 b 34 32))
                                       )
                                   )
                               )
                               (cons nil l) l (append (cdr l) '(( )))
                           )
                       )
                   )
                   ")"
               )
           )
       )
       (vl-string->list str)
   )
)

  • 2 years later...
Posted (edited)

This with sorting list of strings is really mess... After you look whole code posted here : http://forums.augi.com/showthread.php?173343-Sorting-List-either-Alpha-Numeric-or-combined&p=#3

I beleive that you won't know what one sorting is the best for any descent purpose of doing so... Maybe the simplest coded as last example in posted link : (vl-sort l '<) ... Now when I look from different perspective I'd use this, although I am not familiar what was used for sorting - maybe bits of strings which are compared (it looks that ascii table isn't reference here)... So some more clarifications is needed here... Perhaps Lee knows, just guessing... Further more his AlphanumSort would yield perhaps even different than those 4 types from link I provided...

Now I am really confused even more than I previously thought - was thinking that this issue is simple...

 

[EDIT : I've checked it once again - it seems that (vl-sort l '<) is using ascii values - look at the end of posted link : (_sort-1 l) = (_sort-3 l) ; (_sort-4 l) = (vl-sort l '<)]

 

Regards, M.R.

Edited by marko_ribar

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