3
A
回答
4
此代碼提供可在排序算法中使用的'dictionary-lessp
。好像在我的測試工作至今:
(defun dictionary-lessp (str1 str2)
"return t if STR1 is < STR2 when doing a dictionary compare
(splitting the string at numbers and doing numeric compare with them)"
(let ((str1-components (dict-split str1))
(str2-components (dict-split str2)))
(dict-lessp str1-components str2-components)))
(defun dict-lessp (slist1 slist2)
"compare the two lists of strings & numbers"
(cond ((null slist1)
(not (null slist2)))
((null slist2)
nil)
((and (numberp (car slist1))
(stringp (car slist2)))
t)
((and (numberp (car slist2))
(stringp (car slist1)))
nil)
((and (numberp (car slist1))
(numberp (car slist2)))
(or (< (car slist1) (car slist2))
(and (= (car slist1) (car slist2))
(dict-lessp (cdr slist1) (cdr slist2)))))
(t
(or (string-lessp (car slist1) (car slist2))
(and (string-equal (car slist1) (car slist2))
(dict-lessp (cdr slist1) (cdr slist2)))))))
(defun dict-split (str)
"split a string into a list of number and non-number components"
(save-match-data
(let ((res nil))
(while (and str (not (string-equal "" str)))
(let ((p (string-match "[0-9]*\\.?[0-9]+" str)))
(cond ((null p)
(setq res (cons str res))
(setq str nil))
((= p 0)
(setq res (cons (string-to-number (match-string 0 str)) res))
(setq str (substring str (match-end 0))))
(t
(setq res (cons (substring str 0 (match-beginning 0)) res))
(setq str (substring str (match-beginning 0)))))))
(reverse res))))
這是我的測試:
(and (dictionary-lessp "a" "b")
(null (dictionary-lessp "b" "a"))
(null (dictionary-lessp "a" "a"))
(dictionary-lessp "1" "2")
(null (dictionary-lessp "2" "1"))
(null (dictionary-lessp "1" "1"))
(dictionary-lessp "1" "a")
(null (dictionary-lessp "a" "1"))
(dictionary-lessp "" "a")
(null (dictionary-lessp "a" ""))
(dictionary-lessp "ab12" "ab34")
(dictionary-lessp "ab12" "ab123")
(dictionary-lessp "ab12" "ab12d")
(dictionary-lessp "ab132" "ab132z")
(dictionary-lessp "132zzzzz" "ab132z")
(null (dictionary-lessp "1.32" "1ab")))
使用例子是:
(sort '("b" "a" "1" "f19" "f" "f2" "f1can") 'dictionary-lessp)
產量
("1" "a" "b" "f" "f1can" "f2" "f19")
相關問題
- 1. PHP的自然順序排序
- 2. Rails使用自然排序順序排序查找和排序
- 3. 自然或人類排序順序
- 4. LINQ和自然排序順序
- 5. Bash排序類似於Windows的自然排序順序'
- 6. 按自然排序順序排序列表<FileInfo>。
- 7. 按自然順序排列圖像?
- 8. 按字母順序排序,然後按字母順序排列
- 9. 自然排序
- 10. 與-EVAL-後負荷在Emacs Lisp和評價的順序
- 11. Python自然排序
- 12. 自然排序CheckedListbox
- 13. ArrayList自然排序
- 14. 自然排序NHibernate
- 15. sqlalchemy自然排序
- 16. 如何在通用lisp中實現自然排序?
- 17. 如何改變自然排序順序在XSLT
- 18. 如何使用Ember.js SortableMixin以自然順序排序?
- 19. 按自然順序對WordPress分類列表排序
- 20. 按日期排序NSFetchRequest然後按字母順序排序
- 21. 按字母順序排序元素,然後按數字排序
- 22. Emacs-Lisp:如何將emacs-lisp程序打包爲PC應用程序?
- 23. Lisp排序函數
- 24. 表中的自定義排序順序
- 25. OrientDB:OIndexNotUnique的自定義排序順序
- 26. SQL自己的排序順序
- 27. WordPress的 - 自定義排序順序
- 28. perl - 按數字順序降序排列,然後按字母順序排列
- 29. 排序順序
- 30. 排序順序
這是真棒,謝謝! P.S.,我想你忘了一個;;在「p!= 0」之前。 – Ken 2009-12-21 21:36:46
我不明白評論,你在說什麼檢查? – 2009-12-21 22:28:23
文本「p!= 0」看起來像一個評論(在COND裏沒有任何意義),但是在它之前沒有評論標記。 – Ken 2009-12-22 01:30:18