Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort Persian strings for python [duplicate]

Tags:

python

persian

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

sorted( ['ا', 'ب', 'پ', 'ح', 'س', 'ص', 'ف', 'ک', 'ک', 'ک', 'م', 'م']) = 
 ['ا', 'ب', 'ح', 'س', 'ص', 'ف', 'م', 'م', 'پ', 'ک', 'ک', 'ک']
like image 787
majid lesani Avatar asked Jan 31 '19 10:01

majid lesani


2 Answers

try using PyICU:

import PyICU
collator = PyICU.Collator.createInstance(PyICU.Locale('fa_IR.UTF-8'))

print ([i for i in sorted(['ا', 'ب', 'پ', 'ح', 'س', 'ص', 'ف', 'ک', 'ک', 'ک', 'م', 'م'], key=collator.getSortKey)])
like image 83
Ria Avatar answered Oct 01 '22 02:10

Ria


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.

like image 43
Anwarvic Avatar answered Oct 01 '22 04:10

Anwarvic