Avoid using Array.Sort on string arrays

Array.Sort is the first port of call for sorting of data in arrays, however one must be very careful when using it on string arrays: the hidden problem is not just that the default comparison method that Sort will use for strings will take into account locale (culture) and thus make words like say German word “daß” will be deemed equal to “dass”, this might be acceptable behavior under some conditions, however there is a bug in dealing with dashes “-” which appears to be half-acknowledged by Microsoft, but they keep it all very quiet and it is likely that for compatibility reasons they will never fix it.

What this all means is that Array.Sort on a string array may produce sorted results that are valid for a given machine, but the same “sorted” data will be invalid on another machine! This is a terrible situation that can lead to very weird bugs based on assumption that sorted data will remain sorted elsewhere, it won’t! The solution is to avoid using default Array.Sort and either use your own implementation of quick sorting and/or using string.CompareOrdinal to ensure that sorted data stays this way regardless of machine type.

Note that the very same problem applies to usage of string.Compare() – it is this function that causes troubles for default sorting, so be extremely careful when using it, best to avoid it altogether.

Leave a Reply