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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:12:42+00:00 2026-05-11T20:12:42+00:00

I have a generic List<MyClass> where MyClass has a property InvoiceNumber which contains values

  • 0

I have a generic

List<MyClass>

where MyClass has a property InvoiceNumber which contains values such as:

200906/1
200906/2
..
200906/10
200906/11
200906/12

My list is bound to a

BindingList<T>

which supports sorting with linq:

protected override void ApplySortCore(
           PropertyDescriptor property, ListSortDirection direction)
{

    _sortProperty = property;
    _sortDirection = direction;

    var items = this.Items;

    switch (direction)
    {
        case ListSortDirection.Ascending:
            items = items.OrderByDescending(x => property.GetValue(x)).ToList();
            break;
        case ListSortDirection.Descending:
            items = items.OrderByDescending(x => property.GetValue(x)).ToList();
            break;
    }

    this.Items = items;

}

However the default comparer sorts (as supposed) like this:

200906/1
200906/10
200906/11
200906/12
200906/2

which is nasty in this case.

Now I want to use my own IComparer<T> with this. It looks like this:

public class MyComparer : IComparer<Object>
{

    public int Compare(Object stringA, Object stringB)
    {
        String[] valueA = stringA.ToString().Split('/');
        String[] valueB = stringB.ToString().Split('/');

        if(valueA .Length != 2 || valueB .Length != 2)
             return String.Compare(stringA.ToString(), stringB.ToString());

        if (valueA[0] == valueB[0]) 
        {
          return String.Compare(valueA[1], valueB[1]);
        }
        else
        {
          return String.Compare(valueA[0], valueB[0]);
        }

    }

}

and changed the ApplySortCore code to use this IComparer:

case ListSortDirection.Ascending:
    MyComparer comparer = new MyComparer();
    items = items.OrderByDescending(
              x => property.GetValue(x), comparer).ToList();
    break;

When I debug my code, I see that MyComparer.Compare(object, object) is called multiple times and returns the right values (-1, 0, 1) for a compare method.

But my list is still sorted the “wrong” way. Am I missing something? I have no clue.

  • 1 1 Answer
  • 1 View
  • 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-11T20:12:42+00:00Added an answer on May 11, 2026 at 8:12 pm

    Your comparer looks wrong to me. You’re still just sorting in the default text ordering. Surely you want to be parsing the two numbers and sorting based on that:

    public int Compare(Object stringA, Object stringB)
    {
        string[] valueA = stringA.ToString().Split('/');
        string[] valueB = stringB.ToString().Split('/');
    
        if (valueA.Length != 2 || valueB.Length != 2)
        {
            stringA.ToString().CompareTo(stringB.ToString());
        }
    
        // Note: do error checking and consider i18n issues too :)
        if (valueA[0] == valueB[0]) 
        {
            return int.Parse(valueA[1]).CompareTo(int.Parse(valueB[1]));
        }
        else
        {
            return int.Parse(valueA[0]).CompareTo(int.Parse(valueB[0]));
        }
    }
    

    (Note that this doesn’t sit well with your question stating that you’ve debugged through and verified that Compare is returning the right value – but I’m afraid I suspect human error on that front.)

    Additionally, Sven’s right – changing the value of items doesn’t change your bound list at all. You should add:

    this.Items = items;
    

    at the bottom of your method.

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

Sidebar

Related Questions

So, I have a list containing a custom class, MyClass MyClass has properties, which
I have generic list which must be a preserved order so I can retrieve
I have a Generic List object which hold below table as its own value.
I have a generic list of integer and it contains random numbers. How would
i want to use below codes. So i have a generic list<MyClass> i think
I have a generic list view in Django which returns around 300 objects (unless
I have a generic list of objects from which I want to whittle down
I have a Generic list of Objects. Each object has 9 string properties. I
I have a generic list. Some elements of this list belong to a parent
I have a Generic List as below public static readonly List<Customer> Customers = new

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.