Skip to content

Instantly share code, notes, and snippets.

@korakot
Last active October 26, 2017 13:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korakot/2be48b908e67aaed1f99354d99bbca9d to your computer and use it in GitHub Desktop.
Save korakot/2be48b908e67aaed1f99354d99bbca9d to your computer and use it in GitHub Desktop.
A simplified pure-python thaisort
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals, print_function
import re
try:
import icu
thkey = icu.Collator.createInstance(icu.Locale('th_TH')).getSortKey
except ImportError:
def thkey(word):
cv = re.sub('[็-์]', '', word) # remove tone
cv = re.sub('([เ-ไ])([ก-ฮ])', '\\2\\1', cv) # switch lead vowel
tone = re.sub('[^็-์]', ' ', word) # just tone
return cv+tone
# เรียงลำดับข้อมูล list ภาษาไทย
def collation(data):
"""เป็นคำสั่งเรียงลำดับข้อมูลใน ''list'' รับค่า list คืนค่าเป็น ''list''"""
return sorted(data, key=thkey)
if __name__ == "__main__":
a=collation(['ไก่','ไข่','ก','ฮา'])
print(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment