tengte 发表于 2009-11-23 01:49:00

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

<p>  XP的资源管理器对文件名进行排序时,对于含数字的文件名(字符串),会按数值的大小进行排序。如:升序时,“文件2”会排在“文件12”的前面。我也编写了一个功能类似的函数,与大家共享。<br/>例:<br/>_$ (setq sl1 '("4" "5" "6" "1" "2" "3" "10" "11" "12" "7" "8" "9" "13" "14" "15")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sl2 '("4a" "5a" "6a" "1a" "2a" "3a" "10a" "11a" "12a" "7a" "8a" "9a" "13a" "14a" "15a")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sl3 '("A4" "A5" "A6" "A1" "A2" "A3" "A10" "A11" "A12" "A7" "A8" "A9" "A13" "A14" "A15")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sl4 '("B4c" "B5c" "B6c" "B1c" "B2c" "B3c" "B10c" "B11c" "B12c" "B7c" "B8c" "B9c" "B13c" "B14c" "B15c")<br/>&nbsp;&nbsp; )<br/>_$ (TBC:Sort sl1 '&lt;)<br/>("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15")<br/>_$ (TBC:Sort sl2 '&lt;)<br/>("1a" "2a" "3a" "4a" "5a" "6a" "7a" "8a" "9a" "10a" "11a" "12a" "13a" "14a" "15a")<br/>_$ (TBC:Sort sl3 '&lt;)<br/>("A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "A13" "A14" "A15")<br/>_$ (TBC:Sort sl4 '&lt;)<br/>("B1c" "B2c" "B3c" "B4c" "B5c" "B6c" "B7c" "B8c" "B9c" "B10c" "B11c" "B12c" "B13c" "B14c" "B15c")<br/>_$ (TBC:Sort (append sl4 sl3 sl2 sl1) '&lt;)<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-排序方式。“'&lt;”为升序,“'&gt;”为降序。<br/>(defun TBC:Sort&nbsp;(lst func / Split CompFunc tmp)<br/>&nbsp; (defun Split (str / tmp i n p x len lst1 lst)<br/>&nbsp;&nbsp;&nbsp; (setq tmp&nbsp; (vl-string-&gt;list str)<br/>&nbsp;&nbsp; lst1 (mapcar '(lambda (x) (and (&lt;= 48 x) (&lt;= x 57))) tmp)<br/>&nbsp;&nbsp; n&nbsp;&nbsp;&nbsp; (length lst1)<br/>&nbsp;&nbsp; i&nbsp;&nbsp;&nbsp; 0<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; (while (&lt; i n)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq x (nth i lst1)<br/>&nbsp;&nbsp;&nbsp;&nbsp; i (1+ i)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (= i 1)<br/>&nbsp;(setq p&nbsp;1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len 1<br/>&nbsp;) ;else<br/>&nbsp;(if (= x (nth (- i 2) lst1))<br/>&nbsp;&nbsp; (setq len (1+ len)) ;else<br/>&nbsp;&nbsp; (setq&nbsp;tmp (substr str p len)<br/>&nbsp;&nbsp;tmp (if&nbsp;x<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tmp<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (atoi tmp)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;lst (append lst (list tmp))<br/>&nbsp;&nbsp;p&nbsp;&nbsp; i<br/>&nbsp;&nbsp;len 1<br/>&nbsp;&nbsp; )<br/>&nbsp;) ;if<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) ;if<br/>&nbsp;&nbsp;&nbsp; ) ;while<br/>&nbsp;&nbsp;&nbsp; (if&nbsp;(&gt; n 0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq tmp&nbsp;(substr str p len)<br/>&nbsp;&nbsp;&nbsp;&nbsp; tmp&nbsp;(if x<br/>&nbsp;&nbsp;&nbsp; (atoi tmp)<br/>&nbsp;&nbsp;&nbsp; tmp<br/>&nbsp;&nbsp;)<br/>&nbsp;&nbsp;&nbsp;&nbsp; lst&nbsp;(append lst (list tmp))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; lst<br/>&nbsp; )<br/>&nbsp; (defun CompFunc (lst1 lst2 / tmp do flag i n el1 el2 typ1 typ2)<br/>&nbsp;&nbsp;&nbsp; (setq i&nbsp; 0<br/>&nbsp;&nbsp; n&nbsp; (min (length lst1) (length lst2))<br/>&nbsp;&nbsp; do T<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp; (while (and do (&lt;= i n))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq el1&nbsp; (nth i lst1)<br/>&nbsp;&nbsp;&nbsp;&nbsp; typ1 (type el1)<br/>&nbsp;&nbsp;&nbsp;&nbsp; el2&nbsp; (nth i lst2)<br/>&nbsp;&nbsp;&nbsp;&nbsp; typ2 (type el2)<br/>&nbsp;&nbsp;&nbsp;&nbsp; i&nbsp; (1+ i)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (= typ1 typ2)<br/>&nbsp;(setq do&nbsp;&nbsp; (= el1 el2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flag (eval (list func el1 el2))<br/>&nbsp;) ;else<br/>&nbsp;(setq do&nbsp;&nbsp; nil<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flag (or (= typ1 'nil) (= typ2 'STR))<br/>&nbsp;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) ;if<br/>&nbsp;&nbsp;&nbsp; ) ;while<br/>&nbsp;&nbsp;&nbsp; flag<br/>&nbsp; )<br/>&nbsp; (setq&nbsp;tmp (mapcar 'Split lst)<br/>&nbsp;tmp (vl-sort-i tmp 'CompFunc)<br/>&nbsp;tmp (mapcar '(lambda (x) (nth x lst)) tmp)<br/>&nbsp; )<br/>&nbsp; tmp<br/>)</p>

liuhe 发表于 2023-12-16 16:50:49

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")

yk1216 发表于 2024-8-30 22:29:43

收藏了,谢谢分享学习

xhq1954425 发表于 2009-11-23 04:12:00

谢谢分享!下来收藏。

yoyoho 发表于 2009-12-13 14:48:00

<p>感谢楼主分享</p><p>学习了!</p>

yucpp 发表于 2010-5-28 23:41:00

<p>好久不来论坛了。发现这种树形列表看帖子很不方便,咋设置下,恢复到以前的那种状态?</p>
<p>程序很好,谢谢啦!</p>

redcat 发表于 2010-6-27 09:51:00

<p>兄弟,其实不用那么复杂,CAD有个函数可以达到你的目的(acad_strlsort <em class="codeEmphasisMild">list</em>)</p>
<p>再改写一下</p>
<p><font face="Verdana">(defun sort (lst mode) ;lst为表,mode为排序模式<br/>&nbsp; (if mode<br/>&nbsp;&nbsp;&nbsp; (acad_strlsort lst) ;mode为非nil是升序排列<br/>&nbsp;&nbsp;&nbsp; (reverse (acad_strlsort lst)) ;mode为nil是降序排列<br/>&nbsp; ) ;_ 结束if<br/>) ;_ 结束defun</font></p>

hzj526 发表于 2010-6-27 11:03:00

<p><font color="#61b713" face="Verdana"><b>redcat</b></font></p>
<p><strong><font color="#61b713">简单才是最好</font></strong></p>

lsjj 发表于 2010-6-27 11:21:00

redcat发表于2010-6-27 9:51:00static/image/common/back.gif兄弟,其实不用那么复杂,CAD有个函数可以达到你的目的(acad_strlsort list)
再改写一下
(defun sort (lst mode) ;lst为表,mode为排序模式&nbsp; (if mode&nbsp;&nbsp;&nbsp; (acad_strlsort


<p>您確定有達到樓主的目的?</p>

waterchen 发表于 2011-1-3 18:15:11

谢谢分享!下来收藏學習。

ou67169488 发表于 2012-11-28 19:23:34

楼主辛苦了!!

zhuquanmao 发表于 2012-12-8 20:03:25

收藏喽 好用啊 谢谢楼主 学习中
页: [1] 2
查看完整版本: [原创]发一个通用的含数字的字符串排序的函数(类似资源管理器对文件名的排序方式)