There is such an array:

Array ( [0] => Yury [1] => Maxim [2] => Юденок [3] => Мыков ) 

How to sort it so that the 1st were Russian words in alphabetical order, then English?

 sort($arr); echo "<pre>"; print_r($arr); echo "</pre>"; Array ( [0] => Maxim [1] => Yury [2] => Мыков [3] => Юденок ) 

So, but I would like Russian words to be on top.

  • in php, of course, bulk of all kinds of junk, and maybe there is even a function that interests you, but it’s still very likely that it’s not there. Why not write your own? It's elementary - DreamChild
  • Well, maybe for you elementary. - Smash
  • sorry, what's the problem? You do not know any sorting method? Or do not know how to distinguish Latin from Cyrillic characters? - DreamChild
  • 2nd, I understand that they have the code, but how to do it is not very. - Smash

4 answers 4

Achtung! The example I have given is far from ideal.

All this trouble, you can potestit here .

 $arr = array( 'Петя', 'Yury', 'Maxim', 'Юденок', 'Мыков', 'Obama', 'Артем', 'Янукович', 'Яша', 'Gerome' ); function abc($a,$b){ $la = mb_substr($a,0,1,'utf-8'); $lb = mb_substr($b,0,1,'utf-8'); if(ord($la) > 122 && ord($lb) > 122){ return $a > $b ? 1 : -1; } if(ord($la) > 122 || ord($lb) > 122) { return $a < $b ? 1 : -1; } } uasort($arr, 'abc'); // или usort(), если сохранение ключей не важно echo '<pre>'; print_r($arr); echo '</pre>'; // результат: Array ( [6] => Артем [4] => Мыков [0] => Петя [3] => Юденок [7] => Янукович [8] => Яша [9] => Gerome [5] => Obama [2] => Maxim [1] => Yury ) 

    I know that it is not the most ideal solution, but still, maybe, newcomers will come in handy. Sorts at the beginning of the line, after which it can go, anything, except for the line break (".,! -, etc.). In which case, correct it for yourself.

    It differs from the answer of Deonis in that it adequately sorts the Latin alphabetically.

     function localeSort($a,$b) { if (preg_match('/^([а-яё.])+/ui', $a) && preg_match('/^([az.])+/ui', $b)) { return -1; } elseif (preg_match('/^([az.])+/ui', $a) && preg_match('/^([а-яё.])+/ui', $b)) { return 1; } else { return $a < $b ? -1 : 1; } } 

      Obviously, you should make a separate function that will receive the array and sort it by your criteria. Namely, take as an argument an array and callback, which will represent the failure of the comparator function so that your sorting function “knows” how to compare the elements of the array. Not really familiar with php, but I think there is an opportunity to organize something similar. Inside the main function, implement the sorting algorithm. Choose any comparator function to determine the algorithm for comparing the elements of the array, taking into account the alphabetical comparison. As for the alphabetical comparison, a simple regular expression like this: '/ [az] / i' can help you (assuming that every word in the array consists either of Cyrillic characters or Latin characters only), or you need to compare words symbolically - more than sure that php has functions that allow you to get the character code

        Well, or if you do not want to write your bike, use the usort function. An array and a callback function are passed to it, which compares two elements of the array. You can also compare strings with the usual operator > or < , with the string "Kirill" being greater than the string "Kirill".