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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:36:00+00:00 2026-05-29T10:36:00+00:00

I have an extension class that is converting an Observable collection to list and

  • 0

I have an extension class that is converting an Observable collection to list and performing an order by. This logic is working but I need it to sort Alpha-numeric as well. E.g.

In lieu of A1 A10 A2 A3 should be A1 A2 A3 A10.

What is the best way to go about this?

<pre>

public static void Sort<T>(this ObservableCollection<T> collection, Comparison<T> comparison)
    {
        var comparer = new Comparer<T>(comparison);

        List<T> sorted = collection.OrderBy(x => x, comparer).ToList();

        for (int i = 0; i < sorted.Count(); i++)
            collection.Move(collection.IndexOf(sorted[i]), i);
    }


    internal class Comparer<T> : IComparer<T>
{
    private readonly Comparison<T> comparison;

    public Comparer(Comparison<T> comparison)
    {
        this.comparison = comparison;
    }
    #region IComparer<T> Members

    public int Compare(T x, T y)
    {   
        return comparison.Invoke(x, y);
    }
}

</pre>
  • 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-29T10:36:01+00:00Added an answer on May 29, 2026 at 10:36 am

    Parse the string into two parts, the alpha part and the number part. Put the number part into an int, and then compare the int part only when the alpha part is a tie.

    public int Compare(string left, string right) {
      string leftAlpha;
      int leftNum;
      string rightAlpha;
      int rightNum;
    
      GetParts(left, out leftAlpha, out leftNum);
      GetParts(right, out rightAlpha, out rightNum);
    
      if (leftAlpha != rightAlpha) {
         return String.Compare(leftAlpha, rightAlpha);
      }
      else {
         return leftNum.CompareTo(rightNum);
      }
    }
    
    private void GetParts(string str, out string alpha, out string num) {
      alpha = str.Substring(0, 1);
      string numStr = str.Substring(1);
      num = Int32.Parse(numStr);
    }
    

    If you can find a way to expose those two parts as properties on a class, you might be best off using a CollectionView to bind to. Where the SourceCollection is your ObservableCollection, and then add SortDescriptions which correspond to the properties on the class. That could be overkill, but does have a lot of benefits like inserting new elements directly into the right position. You won’t have to do any sorting on the collections explicitly at all.

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

Sidebar

Related Questions

I have a class that modifies data via some extension methods. In order to
I have public variable 'MessagePlaceholder' on MasterPage and a Class that accesses this property
I have an Objective-c class MyClass. In MyClass.m I have a class extension that
I have a relatively large class that I'm working with and it's all worked
I have an existing partial class that has extension methods applied to it, in
I have a large class, which I have divided into several different class extension
I have a class library with some extension methods written in C# and an
I want to ignore executable files that do not have an extension For example:
The CollectionViewSource.GetDefaultView() method is not in Silverlight 3. In WPF I have this extension
I have a C++ class that contains a non-copyable handle. The class, however, must

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.