Custom Collation Ordering

前端 未结 2 1361
小蘑菇
小蘑菇 2021-01-15 09:17

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

I have a list of words:

{ \"badum\", \"         


        
相关标签:
2条回答
  • 2021-01-15 10:00

    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 讨论(0)
  • 2021-01-15 10:06

    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.

    0 讨论(0)
提交回复
热议问题