# Sorting list of Chinese strings

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:

Sort[list2,
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?

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[SystemAlphabeticOrderDumpconvertOptionsToStringSortArguments[
"Language" -> lang, "MainHeader" -> AlphabeticSort],
SystemAlphabeticOrderDumpgetCollatorID[lang]];
idx = SystemAlphabeticOrderDumpcallStringsOrderingFunction[{list2[[All, 1]],
Sequence @@ args}];
list2[[idx]]

• Wow, this is so advanced! I am also interested in the way you Spelunking the code. Oct 8, 2016 at 3:17
• I used PrintDefinitions[]. See this. Oct 8, 2016 at 3:20
• Great tool ! Thanks. But how do you know those functions are under SystemAlphabeticOrderDump`? Oct 8, 2016 at 3:23
• You can mouse over a function name in the definition to see its full name with the contexts. Oct 8, 2016 at 3:25
• 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. Oct 8, 2016 at 14:24