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 8842271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:54:58+00:00 2026-06-14T10:54:58+00:00

I am trying to sorting a generic list of objects using the objects Name

  • 0

I am trying to sorting a generic list of objects using the objects Name property. I am using LINQ and the following expressions doesn’t quite work:

var query = possibleWords.OrderBy(x => x.Name.ToLower()).ToList();
foreach (Word word in query) //possibleWords.OrderBy(word => word.Name))
   {
            listWords.Items.Add(word.Name);
   }

“query” should now contain a list of ordered items, if I understand it correctly and item should be added to the listbox named listWords.

However the output is this:

http://screencast.com/t/s1CkkWfXD4 (sorry for the URL link, but SO has somehow locked me out of my account and I apparently can’t post images with this new one).

The listbox is almost alphabetical but not quite. For some reason “aa” and “aaaa” comes last. What can be the reason, and how to resolve it?

Thanks in advance.

Elaboration by request

This code, when entered in Visual Studio and executed:

        List<Word> words = new List<Word>();

        words.Add(new Word("a"));
        words.Add(new Word("Calculator"));
        words.Add(new Word("aaa"));
        words.Add(new Word("Projects"));
        words.Add(new Word("aa"));
        words.Add(new Word("bb"));
        words.Add(new Word("c"));

        IEnumerable<Word> query = words.OrderBy(x => x.Name.ToLower()).ToList();

        foreach (Word word in query)
        {
            Console.WriteLine(word.Name);
        }

Gives me the following output:

a
bb
c
Calculator
ccc
Projects
aa
aaa

This is not sorted correctly: The first “a” is correct, but the subsequent “aa” and “aaa” entries are sent to the bottom of the list.

I’m not too knowledgeable about character sets and encoding, so possibly I am making a rookie mistake here. But in that case I do not recognize what that might be, and I would be a bit puzzled as to why the first “a” is ordering correctly, but the second and third “aa” and “aaa” is not!

Further elaboration – Word class

[Serializable()]
public class Word
{
    [System.Xml.Serialization.XmlAttribute("Name")]
    public string Name { get; set; }

    public Word(string name)
    {
        Name = name;
    }

    public Word() { } //Parameter less constructor neccessary for serialization

}

Cause and resolution

Like @Douglas suggested, the problem was resolved by supplying the StringComparer.InvariantCultureIgnoreCase comparer to the OrderBy method.

On further research, it seems both the FindAll and OrderBy methods (possibly others) have problems, when using Danish culture (da-DK). There might be other methods or cultures that fail, but da-DK culture and FindAll + OrderBy methods definitely are not working as intended.

The OrderBy method has the problem as described in this thread (wrongful ordering). The FindAll method has a similar, very strange problem: Assume we have a list of entries: a, aa, aaa and aaaa. When using FindAll(x => x.StartsWith(“a”)), it will only return “a” NOT aa, aaa and aaaa. If using StartsWith(“aa”), it will correctly find aa, as well as aaa and aaaa. When using StartWith(“aaa”) it will again not find aaaa, only aaa! This seems to be a bug in the framework.

  • 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-06-14T10:54:59+00:00Added an answer on June 14, 2026 at 10:54 am

    Could you try replacing:

    IEnumerable<Word> query = words.OrderBy(x => x.Name.ToLower()).ToList();
    

    …with:

    IEnumerable<Word> query = words.OrderBy(x => x.Name, 
        StringComparer.InvariantCultureIgnoreCase);
    

    There’s a very small chance that it’s a weird culture issue.

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

Sidebar

Related Questions

I'm trying to implement a paging and sorting list in ASP.NET MVC without using
As I am trying to work on sorting some data using Jquery, I thought
Using javascript sort() method, I am trying to do sorting a list but sorting
Basically I'm trying to create a BASIC sorting program in ruby without using .sort
I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up
I'm trying to sort a list and to check my sorting algorithm is working
I'm trying to build some code for dynamically sorting a Linq IQueryable<>. The obvious
I have a project that is using a generic list of a custom class.
I am trying to get the sorting to work for the DataGridView. The sorting
I am trying to create a few string sorting methods using an interface. The

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.