fs_lzh1988 发表于 2019-3-2 16:12:29

求助大神 请问如何实现数值+字母的排序

我的list 的数据是这样的
!aa
(("a" "b" "L1-01")("a" "b" "L1-02")("a" "b" "B2-02")("a" "b" "B2-01") ("a" "b" "B2-03")("a" "b" "L2-01")("a" "b" "B1-01")("a" "b" "B1-02"))

需要排列顺序是:B→L、前序B时数值大→小、前序L时数值小→大、末尾数值小→大,同时需要小括号整体跟着排序,如下:
(("a" "b" "B3-01") ("a" "b" "B2-01")("a" "b" "B2-02")("a" "b" "B1-01")("a" "b" "B1-02")("a" "b" "L1-01")("a" "b" "L1-02")("a" "b" "L2-01"))

我研究vl-sort很久都没想出办法排序,求大神解决。。。

fs_lzh1988 发表于 2019-3-3 23:09:39

本帖最后由 fs_lzh1988 于 2019-3-3 23:16 编辑

:lol自己想办法解决了,但是感觉很啰嗦,想不到其他方法了
(setq l_nnt (vl-sort l_nnt (function (lambda (x y)

(cond ((< (substr (nth 2 x) 1 1) (substr (nth 2 y) 1 1)) t)       
                                                                                       
( (and (= (substr (nth 2 x) 1 1) (substr (nth 2 y) 1 1) "B")
(> (atof (substr (nth 2 x) 2 1)) (atof (substr (nth 2 y) 2 1)) )) t )

( (and (= (substr (nth 2 x) 1 1) (substr (nth 2 y) 1 1) "B")
(= (atof (substr (nth 2 x) 2 1)) (atof (substr (nth 2 y) 2 1)) )
(< (atof (substr (nth 2 x) 4 2)) (atof (substr (nth 2 y) 4 2)) )) t )   
                              
( (and (= (substr (nth 2 x) 1 1) (substr (nth 2 y) 1 1) "L")
(< (atof (substr (nth 2 x) 2 1)) (atof (substr (nth 2 y) 2 1)) )) t )
       
( (and (= (substr (nth 2 x) 1 1) (substr (nth 2 y) 1 1) "L")
(= (atof (substr (nth 2 x) 2 1)) (atof (substr (nth 2 y) 2 1)) )
(< (atof (substr (nth 2 x) 4 2)) (atof (substr (nth 2 y) 4 2)) )) t )       
                                                                                               
(t NIL)
)                                                                                                                                                               
)
)
)
)


页: [1]
查看完整版本: 求助大神 请问如何实现数值+字母的排序