Is there a ready-made function that sorts Chinese characters (by pinyin or stroke number) in Emacs in an org-table or line by line in regular text?

If not, how should one go about writing such function? (I just need a general direction as guidance, if it is too cumbersome to provide the full functioning code over here.)


1 Answer 1


My code is beyond stackoverflow characters limit. So see https://gist.github.com/redguardtoo/78a68fb6ac914bd89b6f04314819d24b for full code.

Here is a short version,

(defconst my-chinese-pinyin-order-hash
     size 30
     test equal
     data (
           "一" 375 ;; 375 corresponds to pinyin of one hanzi
           "乙" 381
           "二" 81
           "十" 293
           ;; ... skipped ...

(defun my-chinese-compare (w1 w2)
  (let ((i 0)
        (max-len (min (length w1) (length w2)))
        v1 v2

    (while (and (not break) (< i max-len))
      (setq v1 (gethash (substring-no-properties w1 i (1+ i)) my-chinese-pinyin-order-hash 9999))
      (setq v2 (gethash (substring-no-properties w2 i (1+ i)) my-chinese-pinyin-order-hash 9999))
      (unless (eq v1 v2)
        (setq rlt (< v1 v2))
        (setq break t))
      (setq i (1+ i)))

     ((eq i max-len)
      (eq max-len (length w1)))

(defun my-chinese-sort-word-list (word-list)
  (when word-list
    (sort word-list #'my-chinese-compare)))

(message "test: %s" (my-chinese-sort-word-list '("小明" "小红" "张三" "李四" "王二" "大李" "古力娜扎" "迪丽热巴")))

I've contributed patches to he most popular Chinese IME pyim (https://github.com/tumashu/pyim) recently. So I'm familiar with Hanzi (Chinese character) processing. For example, I know https://github.com/tumashu/pyim-basedict provides a dictionary contains all the Chinese characters which are already sorted by pinyin.

My code sort the charcter by its number. The number is roughly the line number of the character's pinyin in pyim-basedict's dictionary file.

In my code, I only sorted the word by 2,500 mostly used Chinese characters which is PRC national standard (https://www.zdic.net/zd/zb/cc1/).

  • Thank you! Can you share a bit about your thinking behind the code? How you approached the problem, the code base you drew inspirations from, etc. It can help aspiring hackers learn how to tackle their own problems.
    – Sati
    May 25, 2020 at 6:26
  • Hmm... Does that mean characters not found in the hash table would not be sorted accordingly?
    – Sati
    May 25, 2020 at 6:33
  • Also your code seems to only deal with characters in regular text format. How do we adopt it to work with org-table as well?
    – Sati
    May 26, 2020 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.