Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 477559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:36:11+00:00 2026-05-13T00:36:11+00:00

I encountered an interesting thing today that I have never noticed before. It appears

  • 0

I encountered an interesting thing today that I have never noticed before. It appears that SQL and LINQ order AlphaNumeric strings differently.

Data table contains rows:
A
G
6
P
1
D
J
2
T
Z
9
F
0

If I perform an Order By in the SQL, I receive the following results:
A
D
F
G
J
P
T
Z
0
1
2
6
9

Now consider this LINQ sample:

class Program
{
    static void Main(string[] args)
    {
        var list = new List<string>()
                       {
                           "A",
                           "G",
                           "6",
                           "P",
                           "1",
                           "D",
                           "J",
                           "2",
                           "T",
                           "Z",
                           "9",
                           "F",
                           "0"
                       };
        Console.WriteLine("Default Order:");
        list.ForEach(s => Console.WriteLine(s));

        Console.WriteLine();
        Console.WriteLine("Sorted Order:");
        foreach (string s in list.OrderBy(f => f))
        {
            Console.WriteLine(s);
        }                        
    }
}

The output for this is
0
1
2
6
9
A
D
F
G
J
P
T
Z

So where SQL places Letters first and Numbers 2nd, LINQ orders Numbers first and Letters 2nd. I put these results in a DataGrid and clicked the header, and sure enough it also orders ala LINQ, so this could be a deeper divide, like at the .NET/Windows level.

The problem I have is that my users expect the ordering behavior they are used to seeing as a result of SQL ordering. How do I get LINQ to behave the same way?

UPDATE

The answer is flagged below, but just for anyone who stumbles onto this later, I wanted to recap, because it took a combination of answers to solve the problem.

1) dcp went immediately where my brain didn’t: the Data Source. The problem turns out to be the difference between IBM’s SQL, which uses EBCDIC sorting, and every other technology in the known universe using ASCII sorting. Thanks IBM for once again reminding me why I am now a .NET developer.

2) Recognizing this, Ahmad provided a very elegant solution that also eluded me: a custom IComparer<string>. I used the code he provided and it sorted the List as desired.

Thanks to both StackOverflow comes through again!

UPDATE 2

After yesterdays posting I got this finished and wanted to share the final results.

The post yesterday was a simple list of single characters, but in reality these were embedded in longer strings. To make this work with longer strings, I changed the original string comparer to a char comparer, and then looped through the strings and compared each character until I either found a mismatch or ran out of characters to compare. Here are the final two Comparer classes:

public class EbcdicCharComparer : IComparer
{
public int Compare(char x, char y)
{
int xNum, yNum;
bool xIsNum = Int32.TryParse(x.ToString(), out xNum);
bool yIsNum = Int32.TryParse(y.ToString(), out yNum);

    // compare numbers
    if (xIsNum && yIsNum)
    {
        return xNum.CompareTo(yNum);
    }

    // compare num to char
    if (xIsNum)
    {
        return 1;
    }

    // compare num to char
    if (yIsNum)
    {
        return -1;
    }

    // compare normally
    return x.CompareTo(y);

}

}

public class EbcdicStringComparer : IComparer
{
public int Compare(string x, string y)
{
var xArr = x.ToCharArray();
var yArr = y.ToCharArray();

    var iterations = xArr.Length > yArr.Length ? yArr.Length : xArr.Length;
    var charComp = new EbcdicCharComparer();
    for (int i = 0; i < iterations; i++)
    {
        var compValue = charComp.Compare(xArr[i], yArr[i]);
        if (compValue != 0)
            return compValue;
    }

    // compare as strings
    return x.CompareTo(y);
}

}

The real data is more like this:

  • 024 A 17
  • 024 A 18
  • 024 A 19
  • 024 1 19
  • 024 C 19A
  • 024 3 3
  • 024 A 3B

And as desired, the comparer now returns the data in this order:

  • 024 A 3B
  • 024 A 17
  • 024 A 18
  • 024 A 19
  • 024 C 19A
  • 024 1 19
  • 024 3 3

Thanks again to all who helped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:36:12+00:00Added an answer on May 13, 2026 at 12:36 am

    You could use the overloaded OrderBy method that accepts an IComparer. The IComparer would compare items to yield the desired result:

    public class CustomComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            double xNum, yNum;
            bool xIsNum = Double.TryParse(x, out xNum);
            bool yIsNum = Double.TryParse(y, out yNum);
    
            // compare numbers
            if (xIsNum && yIsNum)
            {
                return xNum.CompareTo(yNum);
            }
    
            // compare num to string
            if (xIsNum)
            {
                return 1;
            }
    
            // compare num to string
            if (yIsNum)
            {
                return -1;
            }
    
            // compare as strings
            return x.CompareTo(y);
        }
    }
    

    Usage:

    list.OrderBy(i => i, new CustomComparer())
    

    The implementation could’ve used Int32.TryParse for your specific sample data, however I used Double just in case you had a “3.0” in the list or such. Of course, you need to be aware of your data. If the value is too large you’ll need to use the appropriate TryParse method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I encountered interesting issue. That's what I have: 2 images with humanoids on
This is quite an interesting issue I'm having with z-indexes that I've never encountered
I encountered something that I can't understand. I have this code: cout << f1
I encountered a problem. I noticed that a Property (say MyProp ) in my
I encountered an interesting problem today which I thought was not possible in Java.
I encountered an interesting situation today in a program where I inadvertantly assigned an
I encountered an interesting SQL table (column names listed): From_TableName | From_Id | To_TableName
Here is an interesting problem that I encountered in a programming competition: Problem statement:
This is an interesting little thing I've encountered. I was messing around with anonymous
I'm experimenting with functional-style Javascript and have encountered an interesting situation. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.