Search code examples
pythonpersian

sort Persian strings for python


I want to use sort function of python but it does not work well.

sorted( ['ا', 'ب', 'پ', 'ح', 'س', 'ص', 'ف', 'ک', 'ک', 'ک', 'م', 'م']) = 
 ['ا', 'ب', 'ح', 'س', 'ص', 'ف', 'م', 'م', 'پ', 'ک', 'ک', 'ک']

Solution

  • try using PyICU:

    import icu
    collator = icu.Collator.Collator.createInstance(icu.Locale('fa_IR.UTF-8'))
    print ([i for i in sorted(['ا', 'ب', 'پ', 'ح', 'س', 'ص', 'ف', 'ک', 'ک', 'ک', 'م', 'م'], key=collator.getSortKey)])