I want to use sort function of python but it does not work well.
sorted( ['ا', 'ب', 'پ', 'ح', 'س', 'ص', 'ف', 'ک', 'ک', 'ک', 'م', 'م']) =
['ا', 'ب', 'ح', 'س', 'ص', 'ف', 'م', 'م', 'پ', 'ک', 'ک', 'ک']
try using PyICU:
import PyICU
collator = PyICU.Collator.createInstance(PyICU.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With