Custom sort order

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

I have a list of words:

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

which I want to sort in the following order:

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

By arbitrary alphabetical order, I mean that I am working on a constructed language with an alphabet that looks like this: ABZTMIGJLNKSOŚPRFUHDVEÐÞY. The C # RuleBasedCollator found in Java would be perfect! If such a thing does not exist, several pointers to writing a custom algorithm will be evaluated.

Thanks in advance.

+2
c # collation
source share
2 answers

I would definitely start by creating a RuleBasedCollator. Finding out the rules you want is one of the hardest tasks.

There is a project that provides .NET icu bindings that may suit you.

If this does not meet your requirements and you decide to write your own, Unicode Collation Algorithm is a good resource. Keep in mind that natural language sorting conceptually (although many optimizations are possible) includes separate passages with increasing specificity. The first pass will look for the so-called primary differences (usually ignoring differences in the case and some diacritics and punctuation), if there are no differences, and the number of primary units in both lines is the same, then you can make the second pass, this taking into account diacritical differences, if any . Then you handle the differences in the case and finally the punctuation marks.

+3
source share

You can pass a 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 what criteria you want to use (the above is obviously just a regular string comparison).

+2
source share

All Articles