3

I have a page that renders alphabetically a directory of keywords (A, B, C, D....Z, 0, 1...9) and some of the keywords are in Hindi (devanagari).

My php code go through the alphabet array, sorts the keywords by first letter and renders columns for each letter with all the corresponding keywords that start with that same letter/number.

My problem is sorting the hindi alphabet array. My array is:

$hindi=array('क','ख','ग','घ','ङ','च','छ','ज','झ','ञ','ट','ठ','ड','ढ','ण','त','थ','द','ध','न','प','फ','ब','भ','म','य','र','ल','व','श','ष','स','ह','ळ','क','ष','ज्','ञ');

I have, for instance, the following keywords I wish to sort: एशिया खाना पकाना फोटोग्राफी भारतीय मसाला विध

I've tried out some approaches with no success and I'm rendering the hindi keywords just under "Hindi" column and unordered.

Is there a way to sort hindi characters using php?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
McRui
  • 1,879
  • 3
  • 20
  • 31

2 Answers2

2

I assume the normal sorting doesn't work for some reason (the Hindi characters are shared among a couple languages, right?) Here's how to sort based on a user-defined character order

you need to usort() and call recursively to compare the next letter if the first ones match - like this

$words = explode(' ', "एशिया खाना पकाना फोटोग्राफी भारतीय मसाला विध");
usort($words, 'hindiCmp');

function hindiCmp($a, $b) {
    $hindi=array('क','ख','ग','घ','ङ','च','छ','ज','झ','ञ','ट','ठ','ड','ढ','ण','त','थ','द','ध','न','प','फ','ब','भ','म','य','र','ल','व','श','ष','स','ह','ळ','क','ष','ज्','ञ');
    $a1 = array_search(substr($a, 0, 1), $hindi); // first letter of a
    $b1 = array_search(substr($b, 0, 1), $hindi); // first letter of b
    if ($a1 < $b1) {
        return 1;
    } elseif ($a1 > $b1) {
        return -1;
    } else {
        if ((strlen($a) <= 1) && (strlen($b) <= 1)) { //end of both strings?
            return 0; // then it's an exact match
        } else { // otherwise compare the next characters
            return hindiCmp(substr($a, 1), substr($b, 1));
        }
    } 
}

Edit - for the curious- http://en.wikipedia.org/wiki/Nagari

"[Nagari is used by] Several Indian languages, including Sanskrit, Hindi, Marathi, Pahari (Garhwali and Kumaoni), Nepali, Bhili, Konkani, Bhojpuri, Magahi, Kurukh, Nepal Bhasa, and Sindhi. Sometimes used to write or transliterate Sherpa and Kashmiri. Formerly used to write Gujarati."

msEmmaMays
  • 1,073
  • 7
  • 7
  • First, thanks for answering my question. To test it, I've copied and paste into a test page, just like that and what I get is: "Warning: usort() expects parameter 2 to be a valid callback, function 'hindiCmp' not found or invalid function name" – McRui Sep 04 '12 at 11:12
  • I Noticed some of the letters in your text are not in your array of characters- that is going to cause problems with this code. As for the warning you are getting- put the 'function hindiCmp() {}' code above the two lines that call it (This would happen if you placed the hindiCmp function inside of another function instead of in the root file- it could happen on some frameworks depending on how things are included) – msEmmaMays Sep 04 '12 at 17:57
  • Thanks Robert, in fact I tried to put it the logical way - beneath the function but I had code mixed and that's why I also got the error. The function works without errors but it returns "1". I'm sorry, probably not understanding how to use it properly, but what I'm looking for is t get the words effectively ordered by the first letter. Anyways, thank you very much for your time. – McRui Sep 04 '12 at 23:20
  • sorry, there was a typo in my code- $words will be sorted by usort() and return true/false (not the returned array) - check the order of $words after calling usort() on it, it will be different – msEmmaMays Sep 05 '12 at 01:32
  • 1
    Thanks again for the precious help. Now it works and does the sort as I was expecting. Thank you so much. – McRui Sep 05 '12 at 07:35
  • I tried to use it but it does not work as expected. I am kind of sure that it is something with the encoding. Here is the url to my script http://www.devarticles.in/tools/sorthindi.php. I have printed out the output to browser for a better understanding what is happening within the script. I have printed out the pr($a."==".$b); in the outer if/else conditions else part. I have also printed out the $words array in the start and after the end of sorting function has finished execution. I already have set UTF-8 header in the beginning of the script. Posting link to script in anonther comment – Arvind K. Dec 18 '16 at 09:06
  • Here is the link to actual script content. http://www.devarticles.in/tools/sorthindi.html See "The actual sorting functionality is below" section – Arvind K. Dec 18 '16 at 09:12
  • Should I post it as a new question? – Arvind K. Dec 18 '16 at 09:14
0

You may use sort:

sort($keywords, SORT_NATURAL);
Jocelyn
  • 11,209
  • 10
  • 43
  • 60
  • he's not trying to sort his array of characters! he's trying to use that array to sort words, based on the order of the characters in that array... – msEmmaMays Sep 03 '12 at 21:56
  • Thanks. Tis was one of the approaches but unsuccessfully – McRui Sep 04 '12 at 11:19