C # case-sensitive ASCII sort?

I need to sort an Array array and it MUST be sorted by ascii.

if you use Array.Sort (myArray) it will not work.

for example: myArray ("aAzxxxx", "aabxxxx") if using Array.Sort (myArray) the result will be

  • aabxxxx
  • aAzxxxx

but if ascii sort, because A <a, (capital A is 65, a is 97, therefore A <a) the result will be

  • aAzxxxx
  • aabxxxx

this is the result i need. any ideas on how to ASCII sort an Array string?

THX

+6
sorting c # ascii
source share
3 answers

If I understand you correctly, you want to perform a comparison with the ordinal.

Array.Sort(myArray, StringComparer.Ordinal); 
+15
source share

If you need lexical sorting with char code, you can put StringComparer.Ordinal as a comparison with Array.Sort .

 Array.Sort(myArray,StringComparer.Ordinal); 

The StringComparer returned by the Ordinal property performs a simple byte comparison that is language independent. This is most useful when comparing strings that are generated programmatically or when comparing case sensitive resources, such as passwords.

StringComparer class contains several different mappings from which you can choose depending on which culture or case sensitivity you want.

+3
source share

Use a Sort overload that accepts a suitable IComparer<T> :

 Array.Sort(myArray, StringComparer.InvariantCulture); 

This type is case sensitive.

If you are looking for sorting by ASCII value, use StringComparer.Ordinal :

 Array.Sort(myArray, StringComparer.Ordinal); 
+1
source share

All Articles