sort Persian strings for python

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)])

No, it works well... I believe sorted sorts characters based on their associated unicode value. The following is the unicode character for each character:

ا : \u0627
ب : \u0628
ح : \u062d
س : \u0633
ص : \u0635
ف : \u0641
م : \u0645
پ : \u067e
ک : \u06a9

As you can see, the unicode of پ is \u067e while the unicode of ب is \u0628. And the reason for that is the ب is also an Arabic character the same as ا , ح, س, ص, ف, and م. While پ and ک are not.

Tags:

Python

Persian