[原创]发一个通用的含数字的字符串排序的函数(类似资源管理器对文件名的排序方式)
<p> XP的资源管理器对文件名进行排序时,对于含数字的文件名(字符串),会按数值的大小进行排序。如:升序时,“文件2”会排在“文件12”的前面。我也编写了一个功能类似的函数,与大家共享。<br/>例:<br/>_$ (setq sl1 '("4" "5" "6" "1" "2" "3" "10" "11" "12" "7" "8" "9" "13" "14" "15")<br/> sl2 '("4a" "5a" "6a" "1a" "2a" "3a" "10a" "11a" "12a" "7a" "8a" "9a" "13a" "14a" "15a")<br/> sl3 '("A4" "A5" "A6" "A1" "A2" "A3" "A10" "A11" "A12" "A7" "A8" "A9" "A13" "A14" "A15")<br/> sl4 '("B4c" "B5c" "B6c" "B1c" "B2c" "B3c" "B10c" "B11c" "B12c" "B7c" "B8c" "B9c" "B13c" "B14c" "B15c")<br/> )<br/>_$ (TBC:Sort sl1 '<)<br/>("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15")<br/>_$ (TBC:Sort sl2 '<)<br/>("1a" "2a" "3a" "4a" "5a" "6a" "7a" "8a" "9a" "10a" "11a" "12a" "13a" "14a" "15a")<br/>_$ (TBC:Sort sl3 '<)<br/>("A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "A13" "A14" "A15")<br/>_$ (TBC:Sort sl4 '<)<br/>("B1c" "B2c" "B3c" "B4c" "B5c" "B6c" "B7c" "B8c" "B9c" "B10c" "B11c" "B12c" "B13c" "B14c" "B15c")<br/>_$ (TBC:Sort (append sl4 sl3 sl2 sl1) '<)<br/>("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")</p><p>函数如下:<br/>;调用说明:lst-字符串列表;func-排序方式。“'<”为升序,“'>”为降序。<br/>(defun TBC:Sort (lst func / Split CompFunc tmp)<br/> (defun Split (str / tmp i n p x len lst1 lst)<br/> (setq tmp (vl-string->list str)<br/> lst1 (mapcar '(lambda (x) (and (<= 48 x) (<= x 57))) tmp)<br/> n (length lst1)<br/> i 0<br/> )<br/> (while (< i n)<br/> (setq x (nth i lst1)<br/> i (1+ i)<br/> )<br/> (if (= i 1)<br/> (setq p 1<br/> len 1<br/> ) ;else<br/> (if (= x (nth (- i 2) lst1))<br/> (setq len (1+ len)) ;else<br/> (setq tmp (substr str p len)<br/> tmp (if x<br/> tmp<br/> (atoi tmp)<br/> )<br/> lst (append lst (list tmp))<br/> p i<br/> len 1<br/> )<br/> ) ;if<br/> ) ;if<br/> ) ;while<br/> (if (> n 0)<br/> (setq tmp (substr str p len)<br/> tmp (if x<br/> (atoi tmp)<br/> tmp<br/> )<br/> lst (append lst (list tmp))<br/> )<br/> )<br/> lst<br/> )<br/> (defun CompFunc (lst1 lst2 / tmp do flag i n el1 el2 typ1 typ2)<br/> (setq i 0<br/> n (min (length lst1) (length lst2))<br/> do T<br/> )<br/> (while (and do (<= i n))<br/> (setq el1 (nth i lst1)<br/> typ1 (type el1)<br/> el2 (nth i lst2)<br/> typ2 (type el2)<br/> i (1+ i)<br/> )<br/> (if (= typ1 typ2)<br/> (setq do (= el1 el2)<br/> flag (eval (list func el1 el2))<br/> ) ;else<br/> (setq do nil<br/> flag (or (= typ1 'nil) (= typ2 'STR))<br/> )<br/> ) ;if<br/> ) ;while<br/> flag<br/> )<br/> (setq tmp (mapcar 'Split lst)<br/> tmp (vl-sort-i tmp 'CompFunc)<br/> tmp (mapcar '(lambda (x) (nth x lst)) tmp)<br/> )<br/> tmp<br/>)</p> redcat 发表于 2010-6-27 09:51兄弟,其实不用那么复杂,CAD有个函数可以达到你的目的(acad_strlsort list)
再改写一下
(defun sort (ls ...
(SETQ LST (LIST "2LB14-2-2""2LB24-2-1" "2LB2-2-1""2LB2-1" "2LB1-2"
"2LB1-2" "2LB2-2" "2LB2-2-1"
"2LB2-2-3" "2LB22-2-2"
))
(acad_strlsort LST)
("2LB1-2" "2LB1-2" "2LB14-2-2" "2LB2-1" "2LB2-2" "2LB2-2-1" "2LB2-2-1" "2LB2-2-3" "2LB22-2-2" "2LB24-2-1") 收藏了,谢谢分享学习
谢谢分享!下来收藏。 <p>感谢楼主分享</p><p>学习了!</p> <p>好久不来论坛了。发现这种树形列表看帖子很不方便,咋设置下,恢复到以前的那种状态?</p>
<p>程序很好,谢谢啦!</p> <p>兄弟,其实不用那么复杂,CAD有个函数可以达到你的目的(acad_strlsort <em class="codeEmphasisMild">list</em>)</p>
<p>再改写一下</p>
<p><font face="Verdana">(defun sort (lst mode) ;lst为表,mode为排序模式<br/> (if mode<br/> (acad_strlsort lst) ;mode为非nil是升序排列<br/> (reverse (acad_strlsort lst)) ;mode为nil是降序排列<br/> ) ;_ 结束if<br/>) ;_ 结束defun</font></p> <p><font color="#61b713" face="Verdana"><b>redcat</b></font></p>
<p><strong><font color="#61b713">简单才是最好</font></strong></p> redcat发表于2010-6-27 9:51:00static/image/common/back.gif兄弟,其实不用那么复杂,CAD有个函数可以达到你的目的(acad_strlsort list)
再改写一下
(defun sort (lst mode) ;lst为表,mode为排序模式 (if mode (acad_strlsort
<p>您確定有達到樓主的目的?</p> 谢谢分享!下来收藏學習。 楼主辛苦了!! 收藏喽 好用啊 谢谢楼主 学习中
页:
[1]
2