终于考完试了....- ;含数字的字符串排序-----原创 by 无痕.2004.6(defun strbrk (str / strl s1 s2)
- (setq strl (vl-string->list str))
- (mapcar '(lambda(x)(if (<= 48 x 57) (setq s2 (cons x s2)) (setq s1 (cons x s1)))) strl)
- (list (reverse s1) (reverse s2))
- )
- (defun strsort (strlst / lst i)
- (setq lst (mapcar 'strbrk strlst)
- lst (vl-sort lst '(lambda (x y) (< (read (vl-list->string(cadr x)))(read (vl-list->string (cadr y))))))
- i (apply 'max (mapcar '(lambda(x)(length(car x))) lst)))
- (repeat i
- (setq i (1- i)
- lst (vl-sort lst '(lambda (x y) (< (nth i (car x)) (nth i (car y))))))
- )
- (mapcar '(lambda(x)(strcat (vl-list->string (car x))(vl-list->string (cadr x)))) lst)
- )
- ;|
- 已知:
- (setq strlst '("ba2" "ba11" "G1" "G1" "G2" "ba1" "B1"
- "B2" "b1" "b2" "b11" "b12" "B11" "B12"
- "g11" "b" "B" "g12" "g2" "g1" "b"
- "wg11" "B" "G1" "B3" "b3" "b0" "B0"
- "WG1" "WG11" "WG2" "wg1" "wg11" "wg2" "B101"
- "B111" "B99" "b1" "G111" "g22" "g101" "g110"
- "b99" "G33"
- )
- )
- 测试:(strsort strlst) ->
- ("B" "B" "B0" "B1" "B2" "B3" "B11" "B12" "B99" "B101" "B111" "G1" "G1" "G1"
- "G2" "G33" "G111" "WG1" "WG2" "WG11" "b" "b" "b0" "b1" "b1" "b2" "b3" "b11"
- "b12" "b99" "ba1" "ba2" "ba11" "g1" "g2" "g11" "g12" "g22" "g101" "g110" "wg1"
- "wg2" "wg11" "wg11")
- |;
|