Does LINQ sort special characters first?

It seems that when sorting with LINQ, special characters are not considered, and I did not expect this. Anyway, I need to sort the special characters so that they appear first in the list. Any ideas? I know that I can do something like: Use LINQ for random sorting , but how to enable sorting to transmit special characters:

List of examples:

  • "Test"
  • Test

Daniel

+3
source share
3 answers

.Net 3.5 , IComparer. , . , , , IComparer. :

List<string> list = new List<string>();
list.Sort((x, y) =>
{
    if(Char.IsLetterOrDigit(x[0])){
        if(!Char.IsLetterOrDigit(y[0])){
            // x is a letter/digit and y is not, override regular CompareTo
            return -1;
        }
    }
    else if (Char.IsLetterOrDigit(y[0]))
    {
        // y is a letter/digit and x is not, override regular CompareTo
        return 1;
    }
    return x.CompareTo(y);
});
+5

System.Linq.Enumerable OrderBy ThenBy, , IComparer<T>. ( ), , .

+3

qry = qry.OrderBy(q => q.VALUE.Replace("-","aaa"));

It worked for me. It's simple.

0
source

Source: https://habr.com/ru/post/1719448/


All Articles