nameList := "
(
刘备
Washington
徐庶
Chen
A
a
张飞
Franklin
马良
魏延
诸葛亮
关羽
)"
MsgBox, % SortByPinyin(nameList, "`n", "`n")

SortByPinyin(str, deliIn := "`n", deliOut := "`n") {
  ; 拆分成数组
  words := StrSplit(str, deliIn)
  ; 冒泡排序
  loop % words.Length() - 1
    loop % words.Length() - A_Index
      if (PinyinCompare(words[A_Index], words[A_Index + 1]) > 0){
        buf := words[A_Index]
        words[A_Index] := words[A_Index + 1]
        words[A_Index + 1] := buf
      }
  ; 重新组合
  for key, word in words
    sorted .= deliOut word
  return StrReplace(sorted, deliOut,,, 1)
}

PinyinCompare(str1, str2){
  ; 提供中英混排
  static lowers := {"a": 45216.1, "b": 45252.1 ,"c": 45760.1, "d": 46317.1
  , "e": 46825.1, "f": 47009.1, "g": 47296.1, "h": 47613.1, "i": 48118.1
  , "j": 48118.3, "k": 49061.1, "l": 49323.1, "m": 49895.1, "n": 50370.1
  , "o": 50613.1, "p": 50621.1, "q": 50905.1, "r": 51386.1, "s": 51445.1
  , "t": 52217.1, "u": 52697.1, "v": 52697.3, "w": 52697.5, "x": 52979.1, "y": 53688.1, "z": 54480.1}
  static uppers := {"A": 45216.2, "B": 45252.2, "C": 45761.2, "D": 46317.2
    , "E": 46825.2, "F": 47009.2, "G": 47296.2, "H": 47613.2, "I": 48118.2
    , "J": 48118.4, "K": 49061.2, "L": 49323.2, "M": 49895.2, "N": 50370.2
    , "O": 50613.2, "P": 50621.2, "Q": 50905.2, "R": 51386.2, "S": 51445.2
    , "T": 52217.2, "U": 52697.2, "V": 52697.4, "W": 52697.6, "X": 52979.2, "Y": 53688.2, "Z": 54480.2}
  static capcity := VarSetCapacity(buf, 2)
  ; 拆分成单字对比
  chars1 := StrSplit(Trim(str1, " `t`n")), chars2 := StrSplit(Trim(str2, " `t`n"))
  loop % (chars1.Length() < chars2.Length()) ? chars1.Length() : chars2.Length() {
    c1 := chars1[A_Index], c2 := chars2[A_Index]
    if c1 is upper
      code1 := uppers[c1]
    else if c1 is lower
      code1 := lowers[c1]
    else {
      StrPut(c1, &buf, 2, "cp936")
      code1 := (NumGet(buf, 0, "UChar") << 8) + NumGet(buf, 1, "UChar")
    }
    if c2 is upper
      code2 := uppers[c2]
    else if c2 is lower
      code2 := lowers[c2]
    else {
      StrPut(c2, &buf, "cp936")
      code2 := (NumGet(buf, 0, "UChar") << 8) + NumGet(buf, 1, "UChar")
    }
    if (code1 != code2)
      return code1 - code2
  }
  return chars1.Length - chars2.Length
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。