I have a list of Chinese strings; for example, like this:

list1 = StringPartition[Import["http://text-share.com/view/c652fa55", "Data"][[-1]], 4]

Now I want to sort it according to Chinese alphabetical order.

There are two equivalent ways, but they differ in efficiency.

The faster way is

AlphabeticSort[new, Entity["Language", "ChineseMandarin"]]; // AbsoluteTiming
(*{0.00261, Null}*)

a much slower way is

SortBy[new, Transliterate]; // AbsoluteTiming
(*{0.465786, Null}*)

However, what if my data is like this:

list2 = Transpose[{list1, Range @ Length @ list1}];

I want to sort this list by the Chinese strings in it. SortBy[list2, Transliterate @ #[[1]] &] is definitely slow. AlphabeticOrder is also slow:

   AlphabeticOrder[#1[[1]], #2[[1]], 
     Entity["Language", "ChineseMandarin"]]>=0 &]; // AbsoluteTiming
(*{0.512303, Null}*)

Is it possible to use AlphabeticSort to sort an arbitrary list to get maximum efficiency?


1 Answer 1


Spelunking the code of AlphabeticSort[] reveals its mechanism: it generates an index list (similar to Ordering[]) that is then used to sort the original list of strings. Extracting the relevant internal code for constructing this index list, we have:

list1 = StringPartition[Import["http://text-share.com/view/c652fa55", "Data"][[-1]], 4];
list2 = Transpose[{list1, Range @ Length @ list1}];

lang = "ChineseMandarin";
args = Prepend[System`AlphabeticOrderDump`convertOptionsToStringSortArguments[
               "Language" -> lang, "MainHeader" -> AlphabeticSort], 
idx = System`AlphabeticOrderDump`callStringsOrderingFunction[{list2[[All, 1]],
                                                              Sequence @@ args}];
  • $\begingroup$ Wow, this is so advanced! I am also interested in the way you Spelunking the code. $\endgroup$
    – matheorem
    Oct 8, 2016 at 3:17
  • 1
    $\begingroup$ I used PrintDefinitions[]. See this. $\endgroup$ Oct 8, 2016 at 3:20
  • $\begingroup$ Great tool ! Thanks. But how do you know those functions are under System`AlphabeticOrderDump? $\endgroup$
    – matheorem
    Oct 8, 2016 at 3:23
  • $\begingroup$ You can mouse over a function name in the definition to see its full name with the contexts. $\endgroup$ Oct 8, 2016 at 3:25
  • 1
    $\begingroup$ Spelunking is an interesting word I have never seen in English. In Germany we use the word "Spelunke" for a restaurant or inn of dubious reputation. The song title "Honky tonk woman" is sometimes translated into German as "Spelunkenweib". It is of latin/greek origin and refers to a cave. $\endgroup$ Oct 8, 2016 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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