6
$\begingroup$

Array consists of words of different languages. I need to sort elements in reverse alphabetical order and make a list of elements, which consist of english letters.

Example of array which consist of english and russian words:

m = {"computer", "экзамен", "elephant", "стол", "bread", "телефон", 
   "exception", "desktop", "best", "колонка", "zoom", "saphire", 
   "ярость"};

Sorting with Reverse@Sort[m] and Sort[m] work with english words only.

Select[m, LetterQ] can make a list of english words.

Problem is a different language (Russian in this e.g.)

$\endgroup$
  • $\begingroup$ I don't understand this question. Would you please give the output that you require for this sample input? $\endgroup$ – Mr.Wizard Jan 17 '14 at 12:37
  • $\begingroup$ output 1 = {"zoom", "saphire", "exception", "elephant", "desktop", "computer", "bread", "best", "ярость", "экзамен", "телефон", "стол", "колонка"} (reverse alphabetical order) output 2 = {"computer", "elephant", "bread", "exception", "desktop", "best", "zoom", "saphire"} (only english words) $\endgroup$ – Bakkkss Jan 17 '14 at 12:54
  • $\begingroup$ Yes, sorted in reverse alphabetical order $\endgroup$ – Bakkkss Jan 17 '14 at 13:11
7
$\begingroup$
m = {"computer", "экзамен", "elephant", "стол", "bread", "телефон", "exception",
     "desktop", "best", "колонка", "zoom", "saphire", "ярость"};

First Sort it:

sortedm = SortBy[m, First@ToCharacterCode@# &]
{"best", "bread", "computer", "desktop", "elephant", "exception", "saphire", "zoom",
 "колонка", "стол", "телефон", "экзамен", "ярость"}

Then Split it:

splittedm = {englishWords, russianWords} = SplitBy[sortedm, 
 First@ToCharacterCode@"А" <= (First@ToCharacterCode@#) <= First@ToCharacterCode@"я" &]
{{"best", "bread", "computer", "desktop", "elephant", "exception", "saphire", "zoom"}, 
 {"колонка", "стол", "телефон", "экзамен", "ярость"}}

which can be Reversed & Flattened:

Flatten[Reverse /@ splittedm]
{"zoom", "saphire", "exception", "elephant", "desktop", "computer", "bread", "best",
 "ярость", "экзамен", "телефон", "стол", "колонка"}

giving the desired Output 1.

As you wished you can also extract the English/Russian words (even if they are sorted):

englishWords
{"best", "bread", "computer", "desktop", "elephant", "exception", "saphire", "zoom"}

To have the unsorted words use:

{unsortedEnglishWords, unsortedRussianWords} = GatherBy[m, 
 First@ToCharacterCode@"А" <= (First@ToCharacterCode@#) <= First@ToCharacterCode@"я" &]

giving the desired Output 2.


Note:

The ToCharacterCode@"А" and ToCharacterCode@"я" were taken from the Cyrillic script in Unicode Wiki page assuming that you are using the Basic Cyrillic alphabet.

"А" can be obtained with ToString@\:0410 and "я" with ToString@\:044F. Both letters are meant to be first and last elements of the Basic Cyrillic alphabet.

Other alphabets can be used in the same way.

$\endgroup$
  • 1
    $\begingroup$ +1 for detailed yet concise answer. $\endgroup$ – ciao Jan 17 '14 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.