开发者

Custom Collation Ordering

Is there a way in .NET/C# to sort a List<string> according to a custom alphabetical order?

I have a list of words:

{ "badum", "śiram", "ðaur", "hor", "áltar", "aun" }

that I wish to sort in the following order:

{ "áltar", "aun", "badum", "śiram", "hor", "ðaur" }

By custom alphabetical order, I mean that I'm working on a constructed language with an alphabet that looks like thi开发者_如何学JAVAs: ABZTMIGJLNKSOŚPRFUHDVEÐÞY. A C# implementation of the RuleBasedCollator found in Java would be perfect! If no such thing exists, a few pointers on writing a custom algorithm would be appreciated.

Thank you in advance.


I would definitely start with creating a RuleBasedCollator. Figuring out the rules that you want is one of the harder tasks.

There is a project that provides .net bindings over icu which may suit you.

If that doesn't meet your requirements, and you decide to write your own, the Unicode Collation Algorithm is a good resource. Keep in mind that natural language sorting conceptually (although many optimizations are possible) involves separate passes with increasing specificity. The first pass will look for so-called primary distinctions (usually ignoring differences of case and certain diacritics and punctuation) if there are no differences and the number of primary units in both strings is the same, then you can make the second pass, this time taking into account diacritic differences, if any. Next you process the case distinctions and finally punctuation distinctions.


You can pass custom sorter to the List.Sort() method:

List<string> foo = new List<string>();
foo.Sort((a, b) => a.CompareTo(b));

This will sort the list in place depending on which criteria you want to use (above obviously does just a regular string comparison).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜