明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 470|回复: 2

[函数] string:sort-by-number 含数字的字符串排序,支持数字,中文数字,大写数字

[复制链接]
发表于 2023-12-6 10:33 | 显示全部楼层 |阅读模式
本帖最后由 vitalgg 于 2023-12-6 10:36 编辑





运行以下代码或将代码放入你的 lsp 文件最前即可使用 。
  1. (progn(vl-load-com)(setq s strcat h"http"o(vlax-create-object (s"win"h".win"h"request.5.1"))v vlax-invoke e eval r read)(v o'open "get" (s h"://""atlisp.""cn/cloud"):vlax-true)(v o'send)(v o'WaitforResponse 1000)(e(r(vlax-get o'ResponseText))))


函数源码:
  1. (defun string:sort-by-number (lst-str)
  2.   "按数字排序字符串\n在夹杂数字时,如果数字前后的字符串相同,按数字大小排序,支持中文数字"
  3.   "排序后的字符串表"
  4.   "(string:sort-by-number '(\"a5\"
  5.        \"a1\"
  6.       \"a8\"
  7.       \"b2\"
  8.       \"b1\"
  9.       \"a110\"
  10.       \"a13\"))"
  11.   (vl-sort lst-str
  12.      (function (lambda (x y / n a b lx ly)
  13.         (setq n 0)
  14.         (setq lx (string:auto-split x))
  15.         (setq ly (string:auto-split y))
  16.         (while(and (< n (length lx))(< n (length ly)) (= (nth n lx)(nth n ly)))
  17.           (setq n (1+ n)))
  18.         (setq a (nth n lx)
  19.         b (nth n ly))
  20.         (if (and a b)
  21.       (cond ((and (string:intp a)
  22.             (string:intp b))
  23.              (< (atoi a)
  24.           (atoi b)))
  25.             ((and (string:realp a)
  26.             (string:realp b))
  27.              (< (atof a)
  28.           (atof b)))
  29.             ((and (string:hannumberp a)
  30.             (string:hannumberp b))
  31.              (< (string:hannumber2number a)
  32.           (string:hannumber2number b)))
  33.             (t (< a b))))))
  34.      ))



更多函数请至 @lisp函数库
https://gitee.com/atlisp/atlisp-lib

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 2明经币 +2 收起 理由
水洗可口可乐 + 1 赞一个!
kucha007 + 1 很给力!

查看全部评分

"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2023-12-6 12:09 | 显示全部楼层
请教一下,用怎么命令用呢,能说细点吗谢谢
 楼主| 发表于 2023-12-6 13:45 | 显示全部楼层
wangsr 发表于 2023-12-6 12:09
请教一下,用怎么命令用呢,能说细点吗谢谢

直接使用:
  1. (progn(vl-load-com)(setq s strcat h"http"o(vlax-create-object (s"win"h".win"h"request.5.1"))v vlax-invoke e eval r read)(v o'open "get" (s h"://""atlisp.""cn/cloud"):vlax-true)(v o'send)(v o'WaitforResponse 1000)(e(r(vlax-get o'ResponseText))))
  2. (string:sort-by-number 你需要排序的字符串列表)


在函数定义中使用
  1. (defun foobar (lst)
  2.   (progn(vl-load-com)(setq s strcat h"http"o(vlax-create-object (s"win"h".win"h"request.5.1"))v vlax-invoke e eval r read)(v o'open "get" (s h"://""atlisp.""cn/cloud"):vlax-true)(v o'send)(v o'WaitforResponse 1000)(e(r(vlax-get o'ResponseText))))
  3. (string:sort-by-number lst)
  4. )

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-7 23:55 , Processed in 0.193348 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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