明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1379|回复: 1

[函数] [原创]发一个通用的含数字的字符串排序的函数(类似资源管理器对文件名的排序方式)

[复制链接]
明经网友  发表于 2009-11-23 01:44 |阅读模式

  XP的资源管理器对文件名进行排序时,对于含数字的文件名(字符串),会按数值的大小进行排序。如:升序时,“文件2”会排在“文件12”的前面。我也编写了一个功能类似的函数,与大家共享。
例:
_$ (setq sl1 '("4" "5" "6" "1" "2" "3" "10" "11" "12" "7" "8" "9" "13" "14" "15")
      sl2 '("4a" "5a" "6a" "1a" "2a" "3a" "10a" "11a" "12a" "7a" "8a" "9a" "13a" "14a" "15a")
      sl3 '("A4" "A5" "A6" "A1" "A2" "A3" "A10" "A11" "A12" "A7" "A8" "A9" "A13" "A14" "A15")
      sl4 '("B4c" "B5c" "B6c" "B1c" "B2c" "B3c" "B10c" "B11c" "B12c" "B7c" "B8c" "B9c" "B13c" "B14c" "B15c")
   )
_$ (TBC:Sort sl1 '<)
("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15")
_$ (TBC:Sort sl2 '<)
("1a" "2a" "3a" "4a" "5a" "6a" "7a" "8a" "9a" "10a" "11a" "12a" "13a" "14a" "15a")
_$ (TBC:Sort sl3 '<)
("A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "A13" "A14" "A15")
_$ (TBC:Sort sl4 '<)
("B1c" "B2c" "B3c" "B4c" "B5c" "B6c" "B7c" "B8c" "B9c" "B10c" "B11c" "B12c" "B13c" "B14c" "B15c")
_$ (TBC:Sort (append sl4 sl3 sl2 sl1) '<)
("1" "1a" "2" "2a" "3" "3a" "4" "4a" "5" "5a" "6" "6a" "7" "7a" "8" "8a" "9" "9a" "10" "10a" "11" "11a" "12" "12a" "13" "13a" "14" "14a" "15" "15a" "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "A13" "A14" "A15" "B1c" "B2c" "B3c" "B4c" "B5c" "B6c" "B7c" "B8c" "B9c" "B10c" "B11c" "B12c" "B13c" "B14c" "B15c")

函数如下:
;调用说明:lst-字符串列表;func-排序方式。“'<”为升序,“'>”为降序。
(defun TBC:Sort (lst func / Split CompFunc tmp)
  (defun Split (str / tmp i n p x len lst1 lst)
    (setq tmp  (vl-string->list str)
   lst1 (mapcar '(lambda (x) (and (<= 48 x) (<= x 57))) tmp)
   n    (length lst1)
   i    0
    )
    (while (< i n)
      (setq x (nth i lst1)
     i (1+ i)
      )
      (if (= i 1)
 (setq p 1
       len 1
 ) ;else
 (if (= x (nth (- i 2) lst1))
   (setq len (1+ len)) ;else
   (setq tmp (substr str p len)
  tmp (if x
        tmp
        (atoi tmp)
      )
  lst (append lst (list tmp))
  p   i
  len 1
   )
 ) ;if
      ) ;if
    ) ;while
    (if (> n 0)
      (setq tmp (substr str p len)
     tmp (if x
    (atoi tmp)
    tmp
  )
     lst (append lst (list tmp))
      )
    )
    lst
  )
  (defun CompFunc (lst1 lst2 / tmp do flag i n el1 el2 typ1 typ2)
    (setq i  0
   n  (min (length lst1) (length lst2))
   do T
    )
    (while (and do (<= i n))
      (setq el1  (nth i lst1)
     typ1 (type el1)
     el2  (nth i lst2)
     typ2 (type el2)
     i  (1+ i)
      )
      (if (= typ1 typ2)
 (setq do   (= el1 el2)
       flag (eval (list func el1 el2))
 ) ;else
 (setq do   nil
       flag (or (= typ1 'nil) (= typ2 'STR))
 )
      ) ;if
    ) ;while
    flag
  )
  (setq tmp (mapcar 'Split lst)
 tmp (vl-sort-i tmp 'CompFunc)
 tmp (mapcar '(lambda (x) (nth x lst)) tmp)
  )
  tmp
)

回复

使用道具

发表于 2020-9-9 17:21 | 显示全部楼层
本帖最后由 gaics 于 2020-9-9 17:29 编辑

学习。。。。。。。有更简便的方法吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-3 16:02 , Processed in 0.255619 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表