Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderBy with non-english letters give different ordering result on string vs key selector #363

Closed
perason opened this issue Mar 31, 2020 · 5 comments
Labels

Comments

@perason
Copy link

perason commented Mar 31, 2020

The issue I have is that when using a string ("ProductId") for ordering it doesn't sort properly, e.g.

    CultureInfo culture = new CultureInfo("nb-NO");

    itemList = itemList.AsQueryable()
        .OrderBy("ProductId", StringComparer.Create(culture, true))
        .ToList();
Output:
Ååå
Ååå
Aaa
Aaa
Bbb
Bbb

but when using a key selector (s => s.ProductId) it does, e.g.

    CultureInfo culture = new CultureInfo("nb-NO");

    itemList = itemList.AsQueryable()
        .OrderBy(s => s.ProductId, StringComparer.Create(culture, true))
        .ToList();
Output:
Aaa
Aaa
Bbb
Bbb
Ååå
Ååå

I am using System.Linq.Dynamic.Core ver. 1.0.23.

I also tried with StringComparer.InvariantCulture, but its result is the same.

My itemList object(s) looks like this, and is a List<Inventory>

    public class MyModels
    {
        public int Id { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime EditDate { get; set; }
    }

    public class Inventory : MyModels
    {
        public string ProductId { get; set; }
        public string Name { get; set; }
        public string Price { get; set; }
    }
@StefH
Copy link
Collaborator

StefH commented Mar 31, 2020

@perason The code examples you provide are 'normal' linq queries. Can you also provide examples for dynamic linq?

@perason
Copy link
Author

perason commented Mar 31, 2020

I thought I just did. To enable a string ordering as a first OrderBy parameter I added this library, and when not, I am not able to use a string, only a Func<Inventory, TKey> expression.

If this is not a dynamic linq sample, what is?

@StefH
Copy link
Collaborator

StefH commented Mar 31, 2020

Ah I see, sorry for the confusion, your code is like:

.OrderBy("ProductId", StringComparer.Create(culture, true))

However the method will be : https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs#L1316

Which means that the StringComparer will be supplied as a normal argument.
The arguments are used for code like this:

.OrderBy("{0}", "ProductId")

Note that for numbers, there is https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/src/System.Linq.Dynamic.Core/ParsingConfig.cs#L183

Maybe I should add StringComparer or CultureInfo setting so that you choose how it should behave.

@perason
Copy link
Author

perason commented Mar 31, 2020

If you could add how to use StringComparer and CultureInfo I would be grateful, and thanks very much for quick response.

@StefH StefH added the feature label Mar 31, 2020
This was referenced May 26, 2020
@JonathanMagnan
Copy link
Member

Hello @perason ,

The v1.1.2 has been released.

The OrderBynow supports the StringComparer overload.

Best Regards,

Jon


Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval FunctionSQL Eval Function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants