Use an alphanumerical sorting function to interpret & sort numerical data within the string, rather than sorting character-wise, e.g. to sort layouts alphanumerically, you might use:
(defun c:sortlayouts ( / ls1 ls2 ord )
(vlax-for lyt (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
(if (= :vlax-false (vla-get-modeltype lyt))
(setq ls1 (cons (strcase (vla-get-name lyt)) ls1)
ls2 (cons lyt ls2)
)
)
)
(setq ord 0)
(foreach idx (LM:alphanumsort-i ls1)
(vla-put-taborder (nth idx ls2) (setq ord (1+ ord)))
)
(princ)
)
;; 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)
)
)
(vl-load-com) (princ)